aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-05 14:50:37 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-04-05 14:50:37 +0100
commit9d29d0bbcf50e4675a7eee3310041f72478939ab (patch)
treeb94e4e83a193e4ea72cc623e0e556311d8d99f93
parent4e0db79e47971ff2c4b38e7d52c6ed99a849c351 (diff)
parentbaaae40f6ee19da21b77d1da1920eba8053a31a5 (diff)
downloadperlweeklychallenge-club-9d29d0bbcf50e4675a7eee3310041f72478939ab.tar.gz
perlweeklychallenge-club-9d29d0bbcf50e4675a7eee3310041f72478939ab.tar.bz2
perlweeklychallenge-club-9d29d0bbcf50e4675a7eee3310041f72478939ab.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-159/pokgopun/go/ch-1.go44
-rw-r--r--challenge-159/pokgopun/go/ch-2.go124
-rw-r--r--challenge-159/pokgopun/perl/ch-1.pl33
-rw-r--r--challenge-159/pokgopun/perl/ch-2.pl20
4 files changed, 221 insertions, 0 deletions
diff --git a/challenge-159/pokgopun/go/ch-1.go b/challenge-159/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..719d6a8953
--- /dev/null
+++ b/challenge-159/pokgopun/go/ch-1.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ s := argInts()
+ if len(s) == 0 {
+ s = []int{5, 7, 4}
+ }
+ for _, n := range s {
+ fmt.Println("Input: $n =", n)
+ a, b, c, d := 0, 1, 1, n
+ fmt.Printf("Output: %d/%d, %d/%d", a, b, c, d)
+ if n > 1 {
+ for {
+ k := (n + b) / d
+ a, b, c, d = c, d, k*c-a, k*d-b
+ fmt.Printf(", %d/%d", c, d)
+ if c == 1 && d == 1 {
+ break
+ }
+ }
+ }
+ fmt.Printf("\n\n")
+ }
+}
+
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}
diff --git a/challenge-159/pokgopun/go/ch-2.go b/challenge-159/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..d61f36a7aa
--- /dev/null
+++ b/challenge-159/pokgopun/go/ch-2.go
@@ -0,0 +1,124 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ s := argInts()
+ if len(s) == 0 {
+ s = []int{5, 10, 20}
+ }
+ for _, n := range s {
+ fmt.Println("Input: $n =", n)
+ m := map[int]int{}
+ for _, v := range factor(n) {
+ m[v]++
+ }
+ //fmt.Println(m)
+ o := -1
+ if len(m)%2 == 0 {
+ o = 1
+ }
+ for _, v := range m {
+ if v > 1 {
+ o = 0
+ break
+ }
+ }
+ fmt.Printf("Output: %d\n\n", o)
+ }
+}
+
+func factor(n int) (s []int) {
+ if n > 1 {
+ chk, nxt, _ := primeUtil()
+ p := 2
+ if chk(n) {
+ s = append(s, n)
+ } else {
+ for {
+ if n%p != 0 {
+ p = nxt(p)
+ } else {
+ s = append(s, p)
+ n /= p
+ if n == 1 {
+ break
+ } else if chk(n) {
+ s = append(s, n)
+ break
+ }
+ }
+ }
+ }
+ }
+ return s
+}
+func argInts() (s []int) {
+ if len(os.Args) > 1 {
+ for _, v := range os.Args[1:] {
+ i, err := strconv.Atoi(v)
+ if err != nil {
+ log.Fatal(err)
+ }
+ s = append(s, i)
+ }
+ }
+ return s
+}
+func primeUtil() (func(i int) bool, func(i int) int, func()) {
+ m := map[int]bool{}
+ return func(i int) bool {
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check prime from cache ###")
+ return is
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return true
+ }
+ m[i] = false
+ return false
+ }
+ }, func(i int) int {
+ for {
+ i++
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check next number from cache ###")
+ if is {
+ return i
+ } else {
+ continue
+ }
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return i
+ }
+ m[i] = false
+ }
+ }
+ }, func() {
+ fmt.Println("=>", m)
+ }
+}
+func isPrime(n int) bool {
+ //fmt.Println("In isPrime")
+ if n <= 3 {
+ return n > 1
+ } else if n%2 == 0 || n%3 == 0 {
+ return false
+ }
+ for i := 5; i*i <= n; i += 6 {
+ if n%i == 0 || n%(i+2) == 0 {
+ return false
+ }
+ }
+ return true
+}
diff --git a/challenge-159/pokgopun/perl/ch-1.pl b/challenge-159/pokgopun/perl/ch-1.pl
new file mode 100644
index 0000000000..d5fd2115d3
--- /dev/null
+++ b/challenge-159/pokgopun/perl/ch-1.pl
@@ -0,0 +1,33 @@
+use strict;
+use warnings;
+#use Algorithm::Combinatorics qw/combinations/;
+#use Math::Prime::Util qw/is_prime/;
+
+my @n = join("",@ARGV) =~ /^\d+$/ ? @ARGV : (5,7,4);
+
+foreach my $n (@n) {
+ print "Input: \$n = $n\n";
+
+### far too complicated, comes up with this before trying to read about Farey sequence
+# my @fs;
+# if ($n > 1) {
+# @fs = sort{$a->[0]/$a->[1] <=> $b->[0]/$b->[1]} grep{ $_->[0] == 1 || ( (is_prime($_->[0]) || is_prime($_->[1]) ) && $_->[1] % $_->[0] != 0 ) } combinations([1..$n], 2);
+# }
+# @fs = ([0,1], @fs, [1,1]);
+# printf "Output: %s\n\n", join(", ", map{join "/", @$_} @fs );
+
+### here is a simple algorithm found on https://en.wikipedia.org/wiki/Farey_sequence
+ my ($a,$b,$c,$d) = (0,1,1,$n);
+ printf "Output: %d/%d, %d/%d", $a, $b, $c, $d;
+ if ($n > 1){
+ {
+ my $k = int( ($n + $b) / $d );
+ ($a,$b,$c,$d) = ($c,$d,$k*$c-$a,$k*$d-$b);
+ printf", %d/%d", $c, $d;
+ last if $c==1 && $d==1;
+ redo
+ }
+ }
+ print "\n\n";
+
+}
diff --git a/challenge-159/pokgopun/perl/ch-2.pl b/challenge-159/pokgopun/perl/ch-2.pl
new file mode 100644
index 0000000000..b8c4b7fa50
--- /dev/null
+++ b/challenge-159/pokgopun/perl/ch-2.pl
@@ -0,0 +1,20 @@
+use strict;
+use warnings;
+use Math::Factoring qw/factor/;
+
+my @n = join("",@ARGV) =~ /^\d+$/ ? @ARGV : (5,10,20);
+foreach my $n (@n) {
+
+ print "Input: \$n = $n\n";
+
+ my %factor;
+ $factor{$_}++ foreach factor($n);
+
+ my @values = values %factor;
+ my $m = $n == 1 ? 1 :
+ scalar(grep{$_ > 1} @values) ? 0 :
+ @values % 2 ? -1 : 1;
+
+ print "Output: $m\n\n";
+}
+