aboutsummaryrefslogtreecommitdiff
path: root/challenge-156
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-03-15 23:09:50 +0000
committerGitHub <noreply@github.com>2022-03-15 23:09:50 +0000
commit11cb93f95cc1db530b1fdf75d2152f856e1a36d9 (patch)
tree06fc5f25693dd8346d873cee52044c7ba4fc3eb4 /challenge-156
parent2c076bc5300926f26b264db8006fc356f9ab66ce (diff)
parentc14a49440f7a7c9ac1d70f817d5c7a12aa25dba4 (diff)
downloadperlweeklychallenge-club-11cb93f95cc1db530b1fdf75d2152f856e1a36d9.tar.gz
perlweeklychallenge-club-11cb93f95cc1db530b1fdf75d2152f856e1a36d9.tar.bz2
perlweeklychallenge-club-11cb93f95cc1db530b1fdf75d2152f856e1a36d9.zip
Merge pull request #5782 from pokgopun/pwc156
pwc156 solution in GO
Diffstat (limited to 'challenge-156')
-rw-r--r--challenge-156/pokgopun/go/ch-1.go74
-rw-r--r--challenge-156/pokgopun/go/ch-2.go87
-rw-r--r--challenge-156/pokgopun/perl/ch-1.pl30
-rw-r--r--challenge-156/pokgopun/perl/ch-2.pl66
4 files changed, 257 insertions, 0 deletions
diff --git a/challenge-156/pokgopun/go/ch-1.go b/challenge-156/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..44adad9f21
--- /dev/null
+++ b/challenge-156/pokgopun/go/ch-1.go
@@ -0,0 +1,74 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ n := getArgInt(10)
+ var (
+ ps string
+ pc int
+ )
+ chk := checkPrime()
+ for i := 1; i < 10000; i++ {
+ sum := sumb(i)
+ //fmt.Println("binary sum of", i, " is", sum)
+ if chk(sum) {
+ ps += fmt.Sprint(i, ", ")
+ pc++
+ }
+ if pc == n {
+ fmt.Println(ps[:len(ps)-2])
+ break
+ }
+ }
+}
+func sumb(i int) (s int) {
+ for j := i; j > 0; j /= 2 {
+ s += j % 2
+ }
+ return s
+}
+func getArgInt(n int) int {
+ if len(os.Args) > 1 {
+ u, err := strconv.ParseUint(os.Args[1], 10, 64)
+ if err != nil {
+ log.Fatal(err)
+ }
+ n = int(u)
+ }
+ return n
+}
+func checkPrime() func(i int) bool {
+ m := map[int]bool{}
+ return func(i int) bool {
+ is, ok := m[i]
+ if ok {
+ return is
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return true
+ }
+ m[i] = false
+ return false
+ }
+ }
+}
+func isPrime(n int) bool {
+ 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-156/pokgopun/go/ch-2.go b/challenge-156/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..b856d58314
--- /dev/null
+++ b/challenge-156/pokgopun/go/ch-2.go
@@ -0,0 +1,87 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ i := getArgInt(12)
+ d := getDivisors(i)
+ sum := 0
+ for _, v := range d {
+ sum += v
+ }
+ var o int
+ ds := []int{}
+ var sums int
+ if sum > i {
+ o = 1
+ for j := 2; j < len(d); j++ {
+ sos := getCombo(j, d)
+ for _, s := range sos {
+ var sum int
+ for _, v := range s {
+ sum += v
+ }
+ if sum == i {
+ o = 0
+ ds = s
+ sums = sum
+ goto done
+ }
+ }
+ }
+ }
+done:
+ fmt.Println("Input: $n =", i)
+ msg := fmt.Sprint("The proper divisors ", d, " sum to ", sum, " which is ")
+ if sum > i {
+ fmt.Print(msg, "greater than $n")
+ } else {
+ fmt.Print(msg, "less than $n\n")
+ }
+ if sums != 0 {
+ fmt.Println(", but its subset", ds, "sums to", sums)
+ } else if o == 1 {
+ fmt.Println(" and none of its subset sums to", i)
+ }
+ fmt.Println("Output:", o)
+}
+func getArgInt(n int) int {
+ if len(os.Args) > 1 {
+ u, err := strconv.ParseUint(os.Args[1], 10, 64)
+ if err != nil {
+ log.Fatal(err)
+ }
+ n = int(u)
+ }
+ return n
+}
+func getDivisors(n int) (d []int) {
+ for i := 1; i <= n/2; i++ {
+ if n%i == 0 {
+ d = append(d, i)
+ }
+ }
+ return d
+}
+func getCombo(n int, e []int) (r [][]int) {
+ c := []int{}
+ cTree(n, e, c, func(s []int) {
+ r = append(r, s)
+ })
+ return r
+}
+func cTree(n int, e []int, c []int, f func(s []int)) {
+ if len(c) == n || len(c)+len(e) == n {
+ s := append(c, e...)
+ f(s[:n])
+ } else {
+ for i := 0; len(c)+len(e)-i >= n; i++ {
+ cTree(n, e[i+1:], append(c, e[i]), f)
+ }
+ }
+}
diff --git a/challenge-156/pokgopun/perl/ch-1.pl b/challenge-156/pokgopun/perl/ch-1.pl
new file mode 100644
index 0000000000..1a6d96c0ee
--- /dev/null
+++ b/challenge-156/pokgopun/perl/ch-1.pl
@@ -0,0 +1,30 @@
+use strict;
+use warnings;
+use Math::Prime::Util qw/is_prime/;
+
+my $n = shift;
+$n = 10 unless $n && $n =~ /^\d+$/;
+my $i;
+my %is_prime;
+my @prime;
+{
+ my $j = ++$i;
+ my $sum;
+ {
+ $sum += $j % 2;
+ $j = int($j / 2);
+ redo if $j;
+ }
+ if (defined $is_prime{$sum})
+ {
+ push @prime, $i if $is_prime{$sum};
+ } else {
+ $is_prime{$sum} = 0;
+ if (is_prime($sum)){
+ $is_prime{$sum} = 1;
+ push @prime, $i
+ }
+ }
+ redo if @prime < $n;
+}
+printf "%s\n", join(", ",@prime);
diff --git a/challenge-156/pokgopun/perl/ch-2.pl b/challenge-156/pokgopun/perl/ch-2.pl
new file mode 100644
index 0000000000..eff159f7e6
--- /dev/null
+++ b/challenge-156/pokgopun/perl/ch-2.pl
@@ -0,0 +1,66 @@
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $n = shift;
+$n = 12 unless $n && $n =~ /^\d+$/;
+
+my @d = &getDivisors($n);
+#printf "%s\n", join(", ", @d);
+my $sum = eval(join(" + ",@d));
+my $o = 0;
+{
+ last unless $sum > $n;
+ $o = 1;
+ for my $i (2..$#d) {
+ my $ds = [];
+ &cTree([],$i,[@d],$ds);
+ if (&isSumsEqualN($n,$ds)){
+ $o = 0;
+ last;
+ }
+ }
+}
+#printf "Input: \$n = %s, Output: %s\n", $n, $o;
+printf "Input: \$n = %s\n", $n;
+printf "Output: %s\n", $o;
+
+sub isSumsEqualN{
+ my($n,$ds) = @_;
+ for (@$ds) {
+ my $sum = eval(join(" + ", @$_));
+ if ($sum == $n){
+ return 1;
+ }
+ }
+ return 0;
+}
+sub cTree {
+ my($c,$n,$e,$res) = @_;
+ if ( @$c == $n || @$c + @$e == $n ) {
+ my @res = @{[@$c,@$e]}[0..$n-1];
+ if ($res) {
+ push @$res, \@res;
+ } else {
+ printf "%s\n", join(", ",@res);
+ }
+ } else {
+ {
+ my $ct = [@$c,@{$e}[0]];
+ shift @$e if @$e;
+ &cTree($ct,$n,[@$e],$res);
+ redo if @$ct + @$e > $n;
+ }
+ }
+ }
+sub getDivisors() {
+ my $n = shift;
+ my @d;
+ my $i;
+ {
+ $i++;
+ push @d, $i unless $n % $i;
+ redo if $i < int($n/2);
+ }
+ return @d;
+}