From 440ffedbfa07347dbf5326a97bcec4655c718d78 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Wed, 20 Jul 2022 23:09:19 +0700 Subject: pwc174 solution in go --- challenge-174/pokgopun/go/ch-1.go | 61 +++++++++++++++++++++++ challenge-174/pokgopun/go/ch-2.go | 100 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 challenge-174/pokgopun/go/ch-1.go create mode 100644 challenge-174/pokgopun/go/ch-2.go 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) +} -- cgit From 610e35db7a2992927ef1b8a474ddb4870be7a5e0 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Wed, 20 Jul 2022 23:23:22 +0700 Subject: pwc174 solution in perl - minor updates --- challenge-174/pokgopun/perl/ch-1.pl | 16 +++++++++------- challenge-174/pokgopun/perl/ch-2.pl | 12 ++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/challenge-174/pokgopun/perl/ch-1.pl b/challenge-174/pokgopun/perl/ch-1.pl index 8dd9852d2e..f522f4f9f5 100644 --- a/challenge-174/pokgopun/perl/ch-1.pl +++ b/challenge-174/pokgopun/perl/ch-1.pl @@ -15,13 +15,15 @@ sub isDisarium{ } ### take 1st argument as a number and return an array of digits made from it sub digit{ - my $n = shift; - { - unshift @_, $n % 10; - $n = int($n/10); - redo if $n; - } - return @_; + ### use regex is faster + return split //, shift; +# my $n = shift; +# { +# unshift @_, $n % 10; +# $n = int($n/10); +# redo if $n; +# } +# return @_; } ### takes all arguments as numbers and return summation of them sub sum{ diff --git a/challenge-174/pokgopun/perl/ch-2.pl b/challenge-174/pokgopun/perl/ch-2.pl index c5313ff99c..e76094674d 100644 --- a/challenge-174/pokgopun/perl/ch-2.pl +++ b/challenge-174/pokgopun/perl/ch-2.pl @@ -23,8 +23,10 @@ sub rank_permutation(){ $q = int($r / $fact); # by decomposing r = q * fact + rest $r %= $fact; push @p, $digits[$q]; - $digits[$q] = undef; # remove this digit p[i]; - @digits = grep{defined} @digits; +# $digits[$q] = undef; # remove this digit p[i]; +# @digits = grep{defined} @digits; + # use splice instead + splice @digits, $q, 1; $fact /= $n - 1 - $i if $i != $n - 1; # weight of next digit } return @p; @@ -44,8 +46,10 @@ sub permutation_rank{ redo; } $r += $fact * $q; - $digits[$q] = undef; - @digits = grep{defined} @digits; # remove this digit p[i] +# $digits[$q] = undef; +# @digits = grep{defined} @digits; # remove this digit p[i] + # use splice instead + splice @digits, $q, 1; $fact /= $n - 1 - $i; # weight of next digit } return $r -- cgit