diff options
| author | Gurunandan Bhat <gbhat@pobox.com> | 2022-07-30 21:50:16 +0530 |
|---|---|---|
| committer | Gurunandan Bhat <gbhat@pobox.com> | 2022-07-30 21:50:16 +0530 |
| commit | bd81ae52c10c4ac2f63283dd38b2b396f518df12 (patch) | |
| tree | 49eb918bda605a9f0a6bbefe71705d8e2628e682 | |
| parent | 925d4a68cbfd366160835a465ea3dacc774c6203 (diff) | |
| download | perlweeklychallenge-club-bd81ae52c10c4ac2f63283dd38b2b396f518df12.tar.gz perlweeklychallenge-club-bd81ae52c10c4ac2f63283dd38b2b396f518df12.tar.bz2 perlweeklychallenge-club-bd81ae52c10c4ac2f63283dd38b2b396f518df12.zip | |
Solution to Challenge 2 in Perl and Go
| -rw-r--r-- | challenge-175/bhat_gurunandan/go/ch-2.go | 72 | ||||
| -rwxr-xr-x | challenge-175/bhat_gurunandan/perl/ch-2.pl | 58 |
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-175/bhat_gurunandan/go/ch-2.go b/challenge-175/bhat_gurunandan/go/ch-2.go new file mode 100644 index 0000000000..70b380283b --- /dev/null +++ b/challenge-175/bhat_gurunandan/go/ch-2.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func gcd(n int, m int) int { + + for m != 0 { + n, m = m, n%m + } + + return n +} + +func totient(n int) int { + + totient := 1 + + for this := n; this > 1; this-- { + if !(n%this == 0 || gcd(n, this) > 1) { + totient++ + } + } + + return totient +} + +func main() { + + var max int + var err error + + if 2 == len(os.Args) { + max, err = strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatal(err) + } + } else { + log.Fatal("ERROR: Requires at exactly one integer argumemt") + } + + cache := map[int]int{} + perfect := []int{} + this := 2 + + for len(perfect) < max { + + sum, iter := 1, this + for iter > 1 { + + if cache[iter] == 0 { + cache[iter] = totient(iter) + } + iter = cache[iter] + + sum += iter + } + + if this == sum { + perfect = append(perfect, this) + } + + this++ + } + + fmt.Println(perfect) + +} diff --git a/challenge-175/bhat_gurunandan/perl/ch-2.pl b/challenge-175/bhat_gurunandan/perl/ch-2.pl new file mode 100755 index 0000000000..ff0b2c0a69 --- /dev/null +++ b/challenge-175/bhat_gurunandan/perl/ch-2.pl @@ -0,0 +1,58 @@ +#! /usr/bin/env perl + +use 5.36.0; +use integer; + +sub _gcd ($a , $b) { + + while ($b != 0) { + ($a, $b) = ($b, $a % $b); + } + + return $a; +} + +sub totient ($n) { + + my $this = $n; + my $totient = 1; + + while (--$this > 1) { + # + # if we have no remainder, then the number is NOT relative + # prime and no need to calculate the gcd so decrement $this + # and move on + # + next if !($n % $this) || + _gcd ($n, $this) > 1; + + ++ $totient; + } + + return $totient; +} + +die "Needs a number\n" unless my $MAX = $ARGV [0]; + +my %cache = (); # this holds any totients that we have seen before +my @perfect = (); +my $this = 2; + +while (@perfect < $MAX) { + + my $sum = 1; + my $totient = $this; + while ($totient = ($cache {$totient} //= totient ($totient))) { + + $sum += $totient; + last if $totient == 1; + } + + push @perfect, $this + if $this == $sum; + + $this++; +} + +say $_ + 1 . " :\t" . $perfect[$_] + for keys @perfect; |
