diff options
Diffstat (limited to 'challenge-146')
27 files changed, 659 insertions, 0 deletions
diff --git a/challenge-146/arne-sommer/blog.txt b/challenge-146/arne-sommer/blog.txt new file mode 100644 index 0000000000..e830040152 --- /dev/null +++ b/challenge-146/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/fractionally-prime.html diff --git a/challenge-146/arne-sommer/misc/cft.dot b/challenge-146/arne-sommer/misc/cft.dot new file mode 100644 index 0000000000..123624c159 --- /dev/null +++ b/challenge-146/arne-sommer/misc/cft.dot @@ -0,0 +1,16 @@ +digraph foogrph { + "1/3" -> "1/4"; + "1/3" -> "4/3"; + "3/2" -> "3/5"; + "3/2" -> "5/2"; + "1/2" -> "1/3"; + "1/2" -> "3/2"; + "2/3" -> "2/5"; + "2/3" -> "5/3"; + "3/1" -> "3/4"; + "3/1" -> "4/1"; + "2/1" -> "2/3"; + "2/1" -> "3/1"; + "1/1" -> "1/2"; + "1/1" -> "2/1"; +} diff --git a/challenge-146/arne-sommer/perl/10001st-perl b/challenge-146/arne-sommer/perl/10001st-perl new file mode 100755 index 0000000000..ec94aee83b --- /dev/null +++ b/challenge-146/arne-sommer/perl/10001st-perl @@ -0,0 +1,22 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use Math::Prime::Util 'is_prime'; + +my $number = int($ARGV[0] || 10001); + +my $count = 0; +my $candidate = 0; + +while (++$candidate) +{ + next unless is_prime($candidate); + $count++; + + last if $count == $number; +} + +say $candidate; diff --git a/challenge-146/arne-sommer/perl/cft-perl b/challenge-146/arne-sommer/perl/cft-perl new file mode 100755 index 0000000000..0b016e495c --- /dev/null +++ b/challenge-146/arne-sommer/perl/cft-perl @@ -0,0 +1,28 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; + +no warnings qw(experimental::signatures); + +my $fraction = $ARGV[0] // ""; + +die "Not a fraction: $fraction" unless $fraction =~ /^\d+\/\d+$/; + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent ($fraction) +{ + my ($numerator, $denominator) = split("/", $fraction); + + return "0/0" if $numerator == 1 && $denominator == 1; + + $numerator < $denominator + ? return $numerator . "/" . ( $denominator - $numerator ) + : return ($numerator - $denominator ) . "/" . $denominator; +} diff --git a/challenge-146/arne-sommer/perl/ch-1.pl b/challenge-146/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..ec94aee83b --- /dev/null +++ b/challenge-146/arne-sommer/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use Math::Prime::Util 'is_prime'; + +my $number = int($ARGV[0] || 10001); + +my $count = 0; +my $candidate = 0; + +while (++$candidate) +{ + next unless is_prime($candidate); + $count++; + + last if $count == $number; +} + +say $candidate; diff --git a/challenge-146/arne-sommer/perl/ch-2.pl b/challenge-146/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..0b016e495c --- /dev/null +++ b/challenge-146/arne-sommer/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; + +no warnings qw(experimental::signatures); + +my $fraction = $ARGV[0] // ""; + +die "Not a fraction: $fraction" unless $fraction =~ /^\d+\/\d+$/; + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent ($fraction) +{ + my ($numerator, $denominator) = split("/", $fraction); + + return "0/0" if $numerator == 1 && $denominator == 1; + + $numerator < $denominator + ? return $numerator . "/" . ( $denominator - $numerator ) + : return ($numerator - $denominator ) . "/" . $denominator; +} diff --git a/challenge-146/arne-sommer/raku/10001st b/challenge-146/arne-sommer/raku/10001st new file mode 100755 index 0000000000..2bec5d731d --- /dev/null +++ b/challenge-146/arne-sommer/raku/10001st @@ -0,0 +1,7 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int :$number where $number > 0 = 10001); + +my $primes := (1..Inf).grep( *.is-prime ); + +say $primes[$number -1];
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/cft b/challenge-146/arne-sommer/raku/cft new file mode 100755 index 0000000000..bbfb954ea2 --- /dev/null +++ b/challenge-146/arne-sommer/raku/cft @@ -0,0 +1,21 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "0/0" if $numerator == $denominator == 1; + + $numerator < $denominator + ?? return $numerator ~ "/" ~ $denominator - $numerator + !! return $numerator - $denominator ~ "/" ~ $denominator; +}
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/cft-hash b/challenge-146/arne-sommer/raku/cft-hash new file mode 100755 index 0000000000..963ed8c806 --- /dev/null +++ b/challenge-146/arne-sommer/raku/cft-hash @@ -0,0 +1,26 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my %parent = ( '1/4' => '1/3', + '4/3' => '1/3', + '3/5' => '3/2', + '5/2' => '3/2', + '1/3' => '1/2', + '3/2' => '1/2', + '2/5' => '2/3', + '5/3' => '2/3', + '3/4' => '3/1', + '4/1' => '3/1', + '2/3' => '2/1', + '3/1' => '2/1', + '1/2' => '1/1', + '2/1' => '1/1' + ); + +my $parent = %parent{$fraction} // die "No such member: $fraction"; +my $grandparent = %parent{$parent} // die "No such member: $parent"; + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; diff --git a/challenge-146/arne-sommer/raku/ch-1.raku b/challenge-146/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..2bec5d731d --- /dev/null +++ b/challenge-146/arne-sommer/raku/ch-1.raku @@ -0,0 +1,7 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int :$number where $number > 0 = 10001); + +my $primes := (1..Inf).grep( *.is-prime ); + +say $primes[$number -1];
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/ch-2.raku b/challenge-146/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..bbfb954ea2 --- /dev/null +++ b/challenge-146/arne-sommer/raku/ch-2.raku @@ -0,0 +1,21 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "0/0" if $numerator == $denominator == 1; + + $numerator < $denominator + ?? return $numerator ~ "/" ~ $denominator - $numerator + !! return $numerator - $denominator ~ "/" ~ $denominator; +}
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/mkcft b/challenge-146/arne-sommer/raku/mkcft new file mode 100755 index 0000000000..cdb7fb8d07 --- /dev/null +++ b/challenge-146/arne-sommer/raku/mkcft @@ -0,0 +1,40 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (Int $levels where $levels > 0 = 3); + +say 'digraph foogrph {'; + +recurse('1/1'); + +say '}'; + +sub recurse (NuDe $current, $level = 1) +{ + my $left = left-child($current); + my $right = right-child($current); + + say " \"{ $current }\" -> \"{ left-child($current) }\""; + say " \"{ $current }\" -> \"{ right-child($current) }\""; + + return if $level == $levels; + + recurse($left, $level +1); + recurse($right, $level +1); +} + + +sub left-child (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "$numerator/{ $numerator + $denominator }"; +} + +sub right-child (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "{ $numerator + $denominator }/$denominator"; +} diff --git a/challenge-146/laurent-rosenfeld/blog.txt b/challenge-146/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..75b6bbfcdc --- /dev/null +++ b/challenge-146/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/laurent_r/2022/01/perl-weekly-challenge-146-prime-numbers-and-fraction-tree.html diff --git a/challenge-146/laurent-rosenfeld/julia/ch-2.jl b/challenge-146/laurent-rosenfeld/julia/ch-2.jl new file mode 100644 index 0000000000..65a5ec63df --- /dev/null +++ b/challenge-146/laurent-rosenfeld/julia/ch-2.jl @@ -0,0 +1,11 @@ +function find_parent(num, denom) + return num < denom ? [num, denom - num] : + num > denom ? [num - denom, denom] : + ("Error on node $num $denom"); +end + +for test in [ [5, 2], [2, 5], [3, 4], [3, 5], [1, 2] ] + parent = find_parent(test[1], test[2]) + gd_parent = find_parent(parent[1], parent[2]) + println("Node $test has parent $parent and grand-parent $gd_parent") +end diff --git a/challenge-146/laurent-rosenfeld/perl/ch-1.pl b/challenge-146/laurent-rosenfeld/perl/ch-1.pl new file mode 100644 index 0000000000..6d62f8471e --- /dev/null +++ b/challenge-146/laurent-rosenfeld/perl/ch-1.pl @@ -0,0 +1,26 @@ +use strict; +use warnings; +use feature "say"; +use constant MAX => 10_001; + +sub primes { + my $max = shift; + my @primes = (2, 3, 5); + my $count = 3; + my $candidate = $primes[-1]; + while ($count <= $max) { + $candidate += 2; + my $not_prime = 0; + my $sq_cand = sqrt $candidate; + for my $i (@primes) { + $not_prime = 1, last unless $candidate % $i; + last if $i > $sq_cand; + } + next if $not_prime; + push @primes, $candidate; + $count ++; + } + return $primes[$max-1]; +} +my $p = primes(MAX); +say "$p"; diff --git a/challenge-146/laurent-rosenfeld/perl/ch-2.pl b/challenge-146/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..c9b8f20cbe --- /dev/null +++ b/challenge-146/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use feature "say"; + +# for a node x/y less than 1, parent is x/(y-x) +# for a node x/y larger than 1, parent is (x-y)/x + +sub parent { + my ($num, $denom) = @{$_[0]}; + return ["Error"] if $num == $denom; + return $num < $denom ? [$num, $denom - $num] : [$num - $denom, $denom]; +} + +for my $fraction ([5, 2], [2, 5], [3, 4], [3, 5], [2, 1], [1, 1]) { + die "Invalid input node @$fraction" if $$fraction[0] == $$fraction[1]; + my $parent = parent $fraction; + my $gd_parent = parent $parent; + say "for child @$fraction, parent is @$parent and gd-parent is @$gd_parent"; +} diff --git a/challenge-146/laurent-rosenfeld/python/ch-2.py b/challenge-146/laurent-rosenfeld/python/ch-2.py new file mode 100644 index 0000000000..7120a602a7 --- /dev/null +++ b/challenge-146/laurent-rosenfeld/python/ch-2.py @@ -0,0 +1,10 @@ +# for a node x/y less than 1, parent is x/(y-x) +# for a node x/y larger than 1, parent is (x-y)/x + +def find_parent(num, denom): + return [num, denom - num] if num < denom else [num - denom, denom] + +for test in ([5, 2], [2, 5], [3, 4], [3, 5]): + parent = find_parent(test[0], test[1]) + gd_parent = find_parent(parent[0], parent[1]) + print("Node", test, "has parent", parent, "and grand-parent", gd_parent) diff --git a/challenge-146/laurent-rosenfeld/raku/ch-1.raku b/challenge-146/laurent-rosenfeld/raku/ch-1.raku new file mode 100644 index 0000000000..b181fb89cd --- /dev/null +++ b/challenge-146/laurent-rosenfeld/raku/ch-1.raku @@ -0,0 +1,4 @@ +use v6; + +my @primes = grep { .is-prime }, (1, 2, 3, -> $a { $a + 2} ...Inf); +say @primes[10001 - 1]; # Subtract 1 because the array starts at 0 diff --git a/challenge-146/laurent-rosenfeld/raku/ch-2.raku b/challenge-146/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..22976624bc --- /dev/null +++ b/challenge-146/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,12 @@ +use v6; + +# for a node x/y less than 1, parent is x/(y-x) +# for a node x/y larger than 1, parent is (x-y)/x + +sub parent (\num, \denom) { + return num < denom ?? (num, denom - num) !! (num - denom, denom); +} +for (5, 2), (2, 5), (3, 4), (3, 5) -> $fraction { + my $parent = parent |$fraction[0,1]; + my $gd-parent = parent |$parent[0,1]; + say "for child $fraction, parent is $parent and gd-parent is $gd-parent"; diff --git a/challenge-146/laurent-rosenfeld/ring/ch-1.ring b/challenge-146/laurent-rosenfeld/ring/ch-1.ring new file mode 100644 index 0000000000..bd5ecf2178 --- /dev/null +++ b/challenge-146/laurent-rosenfeld/ring/ch-1.ring @@ -0,0 +1,24 @@ +p = primes(10001) +see p + nl + +func primes max + primes = [2, 3, 5] + count = len(primes) + candidate = primes[count] + while count < max + candidate += 2 + is_prime = True + sqrt_cand = sqrt(candidate) + for i in primes + if candidate % i = 0 + is_prime = False + exit + ok + if i > sqrt_cand exit ok + next + if is_prime + add(primes, candidate) + count ++ + ok + end + return primes[max] diff --git a/challenge-146/laurent-rosenfeld/ring/ch-2.ring b/challenge-146/laurent-rosenfeld/ring/ch-2.ring new file mode 100644 index 0000000000..78b9092395 --- /dev/null +++ b/challenge-146/laurent-rosenfeld/ring/ch-2.ring @@ -0,0 +1,21 @@ +# for a node x/y less than 1, parent is x/(y-x) +# for a node x/y larger than 1, parent is (x-y)/x + +for test in [ [5, 2], [2, 5], [3, 4], [3,5], [6, 2], [1, 2] ] + parent = find_parent(test[1], test[2]) + gd_parent = find_parent(parent[1], parent[2]) + see "Node " + to_str(test) + " has parent " + to_str(parent) + + " and grand-parent " + to_str(gd_parent) + nl +next + +func find_parent num, denom + if num < denom + return [num, denom - num] + but denom < num + return [num - denom, denom] + else + return ["Error", ""] + ok + +func to_str input + return "" + input[1] + " " + input[2] diff --git a/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java b/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java new file mode 100644 index 0000000000..1f2b6ed0d2 --- /dev/null +++ b/challenge-146/mohammad-anwar/java/theweeklychallenge/FindPrime.java @@ -0,0 +1,55 @@ +package theweeklychallenge; + +/* + +Week 146: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-146 + +Task #1: 10001st Prime Number + + Write a script to generate the 10001st prime number. + +*/ + +import java.lang.Math; +import junit.framework.TestCase; +import static junit.framework.Assert.*; + +public class FindPrime extends TestCase { + + public static void main(String[] args) { + junit.textui.TestRunner.run(theweeklychallenge.FindPrime.class); + } + + public void testFindPrime() { + assertEquals(104743, findPrime(10001)); + } + + public static int findPrime(int count) { + + int c = 0; + int n = 2; + while (c <= count) { + if (isPrime(n)) { + if (++c == count) { + return n; + } + } + n++; + } + + return 0; + } + + public static boolean isPrime(int n) { + + for(int i=2; i <= Math.sqrt(n); i++) { + if ((n % i) == 0) { + return false; + } + } + + return true; + } +} diff --git a/challenge-146/mohammad-anwar/python/ch-1.py b/challenge-146/mohammad-anwar/python/ch-1.py new file mode 100644 index 0000000000..f41fdf9605 --- /dev/null +++ b/challenge-146/mohammad-anwar/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/python3 + +''' + +Week 146: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-146 + +Task #1: 10001st Prime Number + + Write a script to generate the 10001st prime number. + +''' + +import math +import unittest + +def is_prime(n): + i = 2 + while (i <= int(math.sqrt(n))): + if ((n % i) == 0): + return 0 + i += 1 + + return 1 + +def find_prime(count): + c = 0 + n = 2 + while (c <= count): + if (is_prime(n) == 1): + c += 1 + if (c == count): + return n + n += 1 + +# +# +# Unit test class + +class TestFindPrimeNumber(unittest.TestCase): + + def test_example(self): + self.assertEqual( + find_prime(10001), + 104743, + 'Example' + ) + +unittest.main() diff --git a/challenge-146/mohammad-anwar/swift/ch-1.swift b/challenge-146/mohammad-anwar/swift/ch-1.swift new file mode 100644 index 0000000000..26b5e6b873 --- /dev/null +++ b/challenge-146/mohammad-anwar/swift/ch-1.swift @@ -0,0 +1,69 @@ +import Foundation + +/* + +Week 146: + + https://theweeklychallenge.org/blog/perl-weekly-challenge-146 + +Task #1: 10001st Prime Number + + Write a script to generate the 10001st prime number. + +*/ + +enum ParamError: Error { + case missingIndex + case invalidIndex +} + +do { + let paramCount:Int = Int(CommandLine.argc) + + if paramCount <= 1 { + throw ParamError.missingIndex + } + + let count:Int = Int(CommandLine.arguments[1])! + if count > 0 { + var c:Int = 0 + var n:Int = 2 + while c <= count { + if isPrime(n) == 1 { + c += 1 + if c == count { + print(n) + } + } + n += 1 + } + } + else { + throw ParamError.invalidIndex + } +} +catch ParamError.missingIndex { + print("Missing index count.") +} +catch ParamError.invalidIndex { + print("Invalid index count.") +} +catch let error { + print(error) +} + +// +// +// Functions + +func isPrime(_ n:Int) -> Int { + let j:Int = Int(sqrt(Float(n))) + if j >= 2 { + for i in 2...j { + if n % i == 0 { + return 0 + } + } + } + return 1 +} diff --git a/challenge-146/perlboy1967/perl/ch-1.pl b/challenge-146/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..efe3ddbe44 --- /dev/null +++ b/challenge-146/perlboy1967/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 146 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-146/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › 10001st Prime Number +Submitted by: Mohammad S Anwar + +Write a script to generate the 10001st prime number. + +=cut + +use v5.16; + +use Math::Primality qw(next_prime); + +my $N = shift // 10_001; + +my ($i,$p) = (1,2); + +$p = next_prime($p) while ($i++ < $N); + +printf "%d'th prime = %d\n", $N, $p; diff --git a/challenge-146/perlboy1967/perl/ch-2.pl b/challenge-146/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..8485fbef06 --- /dev/null +++ b/challenge-146/perlboy1967/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 146 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-146/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Curious Fraction Tree +Submitted by: Mohammad S Anwar + +You are given a fraction, member of the tree created similar to the above sample. + +Write a script to find out the parent and grandparent of the given member. +Example 1: + + Input: $member = '3/5'; + Output: parent = '3/2' and grandparent = '1/2' + +Example 2: + + Input: $member = '4/3'; + Output: parent = '1/3' and grandparent = '1/2' + +=cut + +use v5.16; + +my $m = shift // '3/5'; +my $p = getCFTparent($m); +my $g = getCFTparent($p); + +printf "member = '%s' has parent = '%s' and grandparent = '%s'\n", $m, $p, $g; + +sub getCFTparent($) { + my ($n) = @_; + + ($a, $b) = split(/\//, $n); + + if ($a < $b) { + return sprintf('%d/%d', $a, $b - $a); + } elsif ($a > $b) { + return sprintf('%d/%d', $a - $b, $b); + } +} diff --git a/challenge-146/robert-dicicco/perl/ch-2.pl b/challenge-146/robert-dicicco/perl/ch-2.pl new file mode 100644 index 0000000000..54c5e471a8 --- /dev/null +++ b/challenge-146/robert-dicicco/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!perl.exe + +use strict; +use warnings; +use 5.30.0; + +### AUTHOR: Robert DiCicco +### DATE: 05-JAN-2022 +### Challenge #145 Curious Fraction Tree + +my $pval = ''; +my $gpar = ''; +my $argc = scalar(@ARGV); + +die "Requires one fraction argument!\n" if($argc != 1); + +# hash contains fraction and its parent, top is designated by '0' +my %family = ( + '0' => '0', + '1/1' => '0', + '1/2' => '1/1', + '2/1' => '1/1', + '1/3' => '1/2', + '3/2' => '1/2', + '2/3' => '2/1', + '3/1' => '2/1', + '1/4' => '1/3', + '4/3' => '1/3', + '3/5' => '3/2', + '5/2' => '3/2', + '2/5' => '2/3', + '5/3' => '2/3', + '3/4' => '3/1', + '4/1' => '3/1', +); + +if(exists($family{$ARGV[0]})){ + $pval = $family{$ARGV[0]}; + if(exists($family{$pval})){ + $gpar = $family{$pval}; + print("parent = $pval and grandparent = $gpar\n"); + } +} else { + print("$ARGV[0] not found!\n"); +} |
