aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2022-07-20 23:09:19 +0700
committerMichael Manring <michael@manring>2022-07-20 23:09:19 +0700
commit440ffedbfa07347dbf5326a97bcec4655c718d78 (patch)
treecd9fcbf74ddc60eded30f33dd548f6cd4bdea65f
parent87bed19e1777519d8f8ca7845918c0dbb0d4ad9b (diff)
downloadperlweeklychallenge-club-440ffedbfa07347dbf5326a97bcec4655c718d78.tar.gz
perlweeklychallenge-club-440ffedbfa07347dbf5326a97bcec4655c718d78.tar.bz2
perlweeklychallenge-club-440ffedbfa07347dbf5326a97bcec4655c718d78.zip
pwc174 solution in go
-rw-r--r--challenge-174/pokgopun/go/ch-1.go61
-rw-r--r--challenge-174/pokgopun/go/ch-2.go100
2 files changed, 161 insertions, 0 deletions
diff --git a/challenge-174/pokgopun/go/ch-1.go b/challenge-174/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..27506f0db3
--- /dev/null
+++ b/challenge-174/pokgopun/go/ch-1.go
@@ -0,0 +1,61 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-174/
+Task 1: Disarium Numbers
+Submitted by: Mohammad S Anwar
+Write a script to generate first 19 Disarium Numbers.
+
+
+A disarium number is an integer where the sum of each digit raised to the power of its position in the number, is equal to the number.
+
+
+For example,
+
+518 is a disarium number as (5 ** 1) + (1 ** 2) + (8 ** 3) => 5 + 1 + 512 => 518
+*/
+package main
+
+import (
+ "fmt"
+ "strconv"
+)
+
+func main() {
+ count := 19
+ var (
+ dp, sum uint
+ b []byte
+ //skip uint
+ )
+ for i := uint(0); i < 10_000_000; i++ {
+ //fmt.Println("===>", i)
+ b = []byte(strconv.FormatUint(uint64(i), 10))
+ /*
+ sum = 0
+ for _, v := range b {
+ sum += uint(v - 48)
+ }
+ if sum%2 != i%2 {
+ skip++
+ continue
+ }
+ */
+ sum = 0
+ for p, d := range b {
+ d -= 48
+ dp = uint(d)
+ for j := 0; j < p; j++ {
+ dp *= uint(d)
+ }
+ //fmt.Println(d, "^", p+1, "=", dp)
+ sum += dp
+ }
+ if sum == i {
+ //fmt.Println(sum, "-->", count)
+ fmt.Println(sum)
+ count--
+ if count == 0 {
+ break
+ }
+ }
+ }
+ //fmt.Println("skip =", skip)
+}
diff --git a/challenge-174/pokgopun/go/ch-2.go b/challenge-174/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..8b20d6761f
--- /dev/null
+++ b/challenge-174/pokgopun/go/ch-2.go
@@ -0,0 +1,100 @@
+/* https://theweeklychallenge.org/blog/perl-weekly-challenge-174/
+Task 2: Permutation Ranking
+Submitted by: Mohammad S Anwar
+You are given a list of integers with no duplicates, e.g. [0, 1, 2].
+
+Write two functions, permutation2rank() which will take the list and determine its rank (starting at 0) in the set of possible permutations arranged in lexicographic order, and rank2permutation() which will take the list and a rank number and produce just that permutation.
+
+Please checkout this post for more informations and algorithm.
+
+Given the list [0, 1, 2] the ordered permutations are:
+
+0: [0, 1, 2]
+1: [0, 2, 1]
+2: [1, 0, 2]
+3: [1, 2, 0]
+4: [2, 0, 1]
+5: [2, 1, 0]
+and therefore:
+
+permutation2rank([1, 0, 2]) = 2
+
+rank2permutation([0, 1, 2], 1) = [0, 2, 1]
+*/
+package main
+
+import (
+ "bufio"
+ "fmt"
+ "os"
+)
+
+func main() {
+ w := bufio.NewWriter(os.Stdout)
+ for i, v := range permutation(3) {
+ w.WriteString(fmt.Sprintf("%d: %v\n\n", i, v))
+ }
+ w.WriteString(fmt.Sprintf("permutation2rank(%v) = %d\n\n", rank2permutation(3, 2), permutation2rank(rank2permutation(3, 2))))
+ w.WriteString(fmt.Sprintf("rank2permutation(%v, %d) = %v\n\n", rank2permutation(3, 0), 1, rank2permutation(3, 1)))
+ w.Flush()
+}
+
+func permutation(n uint) [][]uint {
+ fact := factorial(n)
+ s := make([][]uint, fact)
+ for r := uint(0); r < fact; r++ {
+ s[r] = rank2permutation(n, r)
+ }
+ return s
+}
+
+func permutation2rank(p []uint) uint {
+ n := uint(len(p))
+ fact := factorial(n - 1)
+ var r uint = 0
+ digits := make([]uint, n)
+ for i := uint(0); i < n; i++ {
+ digits[i] = i
+ }
+ var q uint
+ for i := uint(0); i < n-1; i++ {
+ q = 0
+ for digits[q] != p[i] {
+ q++
+ }
+ r += fact * q
+ digits = append(digits[:q], digits[q+1:]...)
+ fact /= n - 1 - i
+ }
+ return r
+}
+
+func rank2permutation(n, r uint) []uint {
+ fact := factorial(n - 1)
+ if r > fact*n-1 {
+ return []uint{}
+ }
+ digits := make([]uint, uint(n))
+ for i := uint(0); i < n; i++ {
+ digits[i] = i
+ }
+ p := []uint{}
+ var q uint
+ for i := uint(0); i < n; i++ {
+ q = r / fact
+ r %= fact
+ p = append(p, digits[q])
+ digits = append(digits[:q], digits[q+1:]...)
+ if i != n-1 {
+ fact /= n - 1 - i
+ }
+ }
+ return p
+}
+
+func factorial(n uint) uint {
+ if n == 0 {
+ return 1
+ }
+ return n * factorial(n-1)
+}