diff options
Diffstat (limited to 'challenge-168')
45 files changed, 2173 insertions, 5 deletions
diff --git a/challenge-168/luca-ferrari/blog-1.txt b/challenge-168/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..2b0b3a1caa --- /dev/null +++ b/challenge-168/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task1 diff --git a/challenge-168/luca-ferrari/blog-2.txt b/challenge-168/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..a3ce243b5c --- /dev/null +++ b/challenge-168/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task2 diff --git a/challenge-168/luca-ferrari/blog-3.txt b/challenge-168/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..e600750e8c --- /dev/null +++ b/challenge-168/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task1plperl diff --git a/challenge-168/luca-ferrari/blog-4.txt b/challenge-168/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..1423018e36 --- /dev/null +++ b/challenge-168/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task2plperl diff --git a/challenge-168/luca-ferrari/blog-5.txt b/challenge-168/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..79cafaf9e2 --- /dev/null +++ b/challenge-168/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task1plpgsql diff --git a/challenge-168/luca-ferrari/blog-6.txt b/challenge-168/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..65410b3ae8 --- /dev/null +++ b/challenge-168/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2022/06/06/PerlWeeklyChallenge168.html#task2plpgsql diff --git a/challenge-168/luca-ferrari/postgresql/ch-1.plperl b/challenge-168/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..585589ec97 --- /dev/null +++ b/challenge-168/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,40 @@ +-- Perl Weekly Challenge 168 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc168; + +CREATE OR REPLACE FUNCTION +pwc168.task1_plperl( int ) +RETURNS SETOF bigint +AS $CODE$ + my ( $limit ) = @_; + $limit //= 13; + my @perrin = (3, 0, 2); + my $seen = {}; + + my $is_prime = sub { + my ( $number ) = @_; + + for ( 2 .. $number - 1 ) { + return undef if $number % $_ == 0; + } + + return 1; + }; + + while ( $limit > 0 ) { + my $current = $perrin[ -2 ] + $perrin[ -3 ]; + elog( DEBUG, "Limit $n and current is $current" ); + push @perrin, $current; + next if ! $is_prime->( $current ); + next if $seen->{ $current }; + + # found! + $seen->{ $current }++; + return_next( $current ); + $limit--; + } + +return undef; +$CODE$ +LANGUAGE plperl; diff --git a/challenge-168/luca-ferrari/postgresql/ch-1.sql b/challenge-168/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..96bda1b3c5 --- /dev/null +++ b/challenge-168/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,68 @@ +-- Perl Weekly Challenge 168 +-- Task 1 + +CREATE SCHEMA IF NOT EXISTS pwc168; + +CREATE OR REPLACE FUNCTION +pwc168.is_prime( n bigint ) +RETURNS bool +AS $CODE$ +DECLARE + i bigint; +BEGIN + FOR i IN 2 .. n - 1 LOOP + IF n % i = 0 THEN + RETURN FALSE; + END IF; + END LOOP; + + RETURN TRUE; +END +$CODE$ +LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION +pwc168.task1_plpgsql( l bigint default 5000 ) +RETURNS SETOF BIGINT +AS $CODE$ +DECLARE + a bigint; + b bigint; + c bigint; + d bigint; +BEGIN + -- bootstrap + a := 3; + b := 0; + c := 2; + + RETURN NEXT a; + RETURN NEXT b; + RETURN NEXT c; + + WHILE l > 0 LOOP + d := a + b; + a := b; + b := c; + c := d; + + RAISE INFO 'Level % value %', l, c; + RETURN NEXT c; + l := l - 1; + END LOOP; + + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; + + +-- use more than 50 to get all the numbers +-- BUT THIS CAN BE VERY SLOW from 70 and beyond! +SELECT DISTINCT n +FROM pwc168.task1_plpgsql( 50 ) n +WHERE pwc168.is_prime( n ) +ORDER BY 1 +LIMIT 13; diff --git a/challenge-168/luca-ferrari/postgresql/ch-2.plperl b/challenge-168/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..e150136c3e --- /dev/null +++ b/challenge-168/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,52 @@ +-- Perl Weekly Challenge 168 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc168; + +CREATE OR REPLACE FUNCTION +pwc168.task2_plperl( int ) +RETURNS int +AS $CODE$ + +my ( $value ) = @_; + +my $is_prime = sub { + my ( $number ) = @_; + + for ( 2 .. $number - 1 ) { + return 0 if ( $number % $_ == 0 ); + } + + return 1; +}; + +my $prime_factors = sub { + my ( $number ) = @_; + my @factors; + + return if $is_prime->( $number ); + + for ( 2 .. $number - 1 ) { + next if ! $is_prime->( $_ ); + next if $number % $_ != 0; + next if $_ > $number; + + while ( ( $number % $_ ) == 0 ) { + push @factors, $_; + $number /= $_; + } + } + + return @factors; +}; + + +my $value = join( '', $prime_factors->( $value ) ); +while ( ! $is_prime->( $value ) ) { + $value = join( '', $prime_factors->( $value ) ); +} + +return $value; + +$CODE$ +LANGUAGE plperl; diff --git a/challenge-168/luca-ferrari/postgresql/ch-2.sql b/challenge-168/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..c0bffbf930 --- /dev/null +++ b/challenge-168/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,69 @@ +-- Perl Weekly Challenge 168 +-- Task 2 + +CREATE SCHEMA IF NOT EXISTS pwc168; + +CREATE OR REPLACE FUNCTION +pwc168.task2_prime_factors( n int ) +RETURNS SETOF int +AS $CODE$ +DECLARE + i int; + p bool; +BEGIN + + FOR i IN 2 .. n - 1 LOOP + p := pwc168.is_prime( i ); + + IF p AND n % i = 0 THEN + WHILE n % i = 0 LOOP + n := n / i; + RETURN NEXT i; + END LOOP; + END IF; + END LOOP; + +RETURN; +END +$CODE$ +LANGUAGE plpgsql; + + +/* +testdb=> select * from pwc168.task2_plpgsql( 10 ); +task2_plpgsql +--------------- +773 + +*/ + +CREATE OR REPLACE FUNCTION +pwc168.task2_plpgsql( n int DEFAULT 10 ) +RETURNS int +AS $CODE$ +DECLARE + i int; + v text; + p bool; +BEGIN + v = '0'; + FOR i IN SELECT * FROM pwc168.task2_prime_factors( n ) LOOP + v := v || i; + END LOOP; + + + p := pwc168.is_prime( v::int ); + + WHILE NOT p LOOP + i := v::int; + v = '0'; + FOR i IN SELECT * FROM pwc168.task2_prime_factors( i ) LOOP + v := v || i; + END LOOP; + p := pwc168.is_prime( v::int ); + END LOOP; + + RETURN v::int; +END +$CODE$ +LANGUAGE plpgsql; diff --git a/challenge-168/luca-ferrari/raku/ch-1.p6 b/challenge-168/luca-ferrari/raku/ch-1.p6 new file mode 100755 index 0000000000..7059667b9b --- /dev/null +++ b/challenge-168/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,21 @@ +#!raku + +# Perl Weekly Challenge 168 + +sub MAIN( Int $limit where { $limit > 1 } = 13 ) { + + # this requires too much time! + # my @perrin = 3, 0, 2, -> $left, $right { $left + $right } ... *; + # my @perrin-primes = @perrin.grep( *.is-prime ); + # @perrin-primes.unique.head( 3 ).join( "\n" ).say; + + my @perrin = 3, 0, 2; + my @perrin-primes; + + while ( @perrin-primes.elems < $limit ) { + @perrin.push: @perrin[ * - 2 ] + @perrin[ * - 3 ]; + @perrin-primes.push: @perrin[ * - 1 ] if @perrin[ * - 1 ].is-prime && ! @perrin-primes.grep( @perrin[ * - 1 ] ); + } + + @perrin-primes.sort.join( "\n" ).say; +} diff --git a/challenge-168/luca-ferrari/raku/ch-2.p6 b/challenge-168/luca-ferrari/raku/ch-2.p6 new file mode 100755 index 0000000000..a19f99a7ca --- /dev/null +++ b/challenge-168/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,35 @@ +#!raku + +# Perl Weekly Challenge 168 + +sub prime-factors( Int $n where { $n > 0 } ) +{ + my $number = $n; + my @factors; + return $n if $n.is-prime; + + for 2 ..^ $n { + next if ! $_.is-prime; + next if $number !%% $_; + next if $_ > $number; + + while ( $number %% $_ ) { + @factors.push: $_; + $number /= $_; + } + + } + + return @factors; +} + +sub HP( Int $n where { $n > 0 } ) +{ + my $number = prime-factors( $n ).join.Int; + return $number if $number.is-prime; + return HP( $number ); +} + +sub MAIN( Int $n where { $n > 1 } = 10 ) { + say HP( $n ); +} diff --git a/challenge-168/mark-anderson/raku/ch-1.raku b/challenge-168/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..e5680d97a2 --- /dev/null +++ b/challenge-168/mark-anderson/raku/ch-1.raku @@ -0,0 +1,5 @@ +#!/usr/bin/env raku + +my $p := 3, 0, 2, -> $a, $b, $ { $a + $b } ... *; + +say $p.grep(&is-prime).unique.head(13).sort; diff --git a/challenge-168/mark-anderson/raku/ch-2.raku b/challenge-168/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..4f5c43de9e --- /dev/null +++ b/challenge-168/mark-anderson/raku/ch-2.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +# This doesn't work for all numbers but it's a start. + +use Prime::Factor; + +say (2..79).map(&home-prime); + +multi home-prime($n where $n == 49|77) { Any } + +multi home-prime($n is copy) +{ + $n = prime-factors($n).join.Int; + return $n if $n.is-prime; + home-prime($n) +} diff --git a/challenge-168/peter-campbell-smith/blog.txt b/challenge-168/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..0a6ae9b139 --- /dev/null +++ b/challenge-168/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html diff --git a/challenge-168/peter-campbell-smith/perl/ch-1.pl b/challenge-168/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..e82f952944 --- /dev/null +++ b/challenge-168/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-06-06 +# PWC 168 task 1 + +use v5.28; +use strict; +use warnings; +use utf8; +use Math::Prime::Util 'is_prime'; +use Math::BigInt lib => 'GMP'; + +# The Perrin sequence is defined to start with [3, 0, 2]; after that, term N is the +# sum of terms N-2 and N-3. (So it continues 3, 2, 5, 5, 7, ….) +# A Perrin prime is a number in the Perrin sequence which is also a prime number. +# Calculate the first 13 Perrin Primes. +# f(13) = [2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977] + +# Blog at https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html + +my (@perrin, $j, $result, $count); + +# initialise +@perrin = (3, 0, 2); +$result = qq[, 2, 3, ]; +$count = 2; + +# calculate perrin primes until 13 unique ones found +for $j (3 .. 9999) { + + # get next term + $perrin[$j] = Math::BigInt->new($perrin[$j - 2]); + $perrin[$j]->badd($perrin[$j - 3]); + + # no good unless prime and not already seen + next unless is_prime($perrin[$j]); + next if $result =~ m|, $perrin[$j],|; + + # add to results + $result .= qq[$perrin[$j], ]; + last if $count ++ == 12; +} + +# deliver the answer +say qq[f($count) = ] . substr($result, 2, -2); diff --git a/challenge-168/peter-campbell-smith/perl/ch-2.pl b/challenge-168/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..3caf8583dc --- /dev/null +++ b/challenge-168/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2022-06-06 +# PWC 168 task 2 + +use v5.28; +use strict; +use warnings; +use utf8; +use Math::Prime::Util qw[factor is_prime]; + +# You are given an integer greater than 1. +# Write a script to find the home prime of the given number. + +# In number theory, the home prime HP(n) of an integer n greater than 1 is the prime +# number obtained by repeatedly factoring the increasing concatenation of prime factors +# including repetitions. + +# Blog at https://pjcs-pwc.blogspot.com/2022/06/more-funny-numbers-and-very-big-one.html + +my ($test, $hp); + +# loop over 2 to 48 (49 is known to be difficult!) +for $test (2 .. 48) { + $hp = home($test); + say qq[hp($test) = $hp]; +} + +sub home { + + my (@prime_factors, $concat); + + # get prime factors + @prime_factors = factor(shift); + + # concatenate them + $concat .= $_ for @prime_factors; + + # either we have an answer or need to go round again + return $concat if is_prime($concat); + home($concat); +} diff --git a/challenge-168/roger-bell-west/javascript/ch-1.js b/challenge-168/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..d3fc9016b2 --- /dev/null +++ b/challenge-168/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,81 @@ +#! /usr/bin/node +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0] + for( var key in a) count[0]++ + for( var key in b) count[1]++ + if( count[0]-count[1] != 0) {return false} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false} + } + return true + } + else + { + return a === b + } +} + +function isprime(candidate) { + if (candidate<2) { + return false + } else if (candidate==2) { + return true + } else if (candidate==3) { + return true + } else if (candidate % 2 == 0) { + return false + } else if (candidate % 3 == 0) { + return false + } + let anchor=0 + let limit=Math.floor(Math.sqrt(candidate)) + while (1) { + anchor+=6 + for (let t = anchor-1; t <= anchor+1; t += 2) { + if (t > limit) { + return true + } + if (candidate % t == 0) { + return false + } + } + } +} + +function perrinprime(n) { + let out = new Set; + let seq = [3, 0, 2]; + while (true) { + let nv = seq[0] + seq[1]; + seq.shift(); + seq.push(nv); + if (isprime(nv)) { + out.add(nv); + if (out.size >= n) { + break; + } + } + } + let o = []; + for (let p of out) { + o.push(p); + } + o.sort((a,b) => a-b); + return o; +} + +if (deepEqual(perrinprime(13),[2, 3, 5, 7, 17, 29, 277, 367, 853, 14197, 43721, 1442968193, 792606555396977])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-168/roger-bell-west/javascript/ch-2.js b/challenge-168/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..99486914e4 --- /dev/null +++ b/challenge-168/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,93 @@ +#! /usr/bin/node + +function genprimes(mx) { + let primesh=new Set([2,3]) + for (let i = 6; i <= mx; i += 6) { + for (let j = i-1; j <= i+1; j += 2) { + if (j <= mx) { + primesh.add(j); + } + } + } + let q=[2,3,5,7]; + let p=q.shift(); + let mr=Math.floor(Math.sqrt(mx)); + while (p <= mr) { + if (primesh.has(p)) { + let i=p*p + for (let i=p*p; i <= mx; i += p) { + primesh.delete(i); + } + } + if (q.length < 2) { + q.push(q[q.length-1]+4); + q.push(q[q.length-1]+2); + } + p=q.shift(); + } + let primes=[...primesh]; + primes.sort(function(a,b) { + return a-b; + }); + return primes; +} + +function primefactor (n) { + let f=new Map() + let m=n + for (let p of genprimes(1+Math.floor(Math.sqrt(n)))) { + while (m % p == 0) { + m=Math.floor(m/p) + if (f.has(p)) { + f.set(p,f.get(p)+1) + } else { + f.set(p,1) + } + if (m == 1) { + break + } + } + } + if (m > 1) { + if (f.has(m)) { + f.set(m,f.get(m)+1) + } else { + f.set(m,1) + } + } + return f +} + +function homeprime (n0) { + let n = n0; + while (true) { + let t = primefactor(n); + if (t.size == 1 && [...t.values()][0] == 1) { + break; + } + let ns = ""; + let tk = [...t.keys()]; + tk.sort((a,b) => a-b); + for (let d of tk) { + for (let i=0; i<t.get(d); i++) { + ns = ns.concat(d); + } + } + n = parseInt(ns); + } + return n; +} + +if (homeprime(10) == 773) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (homeprime(16) == 31636373) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-168/roger-bell-west/kotlin/ch-1.kt b/challenge-168/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..a8628b4c2d --- /dev/null +++ b/challenge-168/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,58 @@ +import kotlin.math.* + +fun isprime(candidate: Long): Boolean { + if (candidate < 2L) { + return false + } else if (candidate==2L) { + return true + } else if (candidate==3L) { + return true + } else if (candidate % 2L == 0L) { + return false + } else if (candidate % 3L == 0L) { + return false + } + var anchor=0L + val limit = Math.sqrt(candidate.toDouble()).toLong() + while (true) { + anchor += 6L + for (t in anchor-1L..anchor+1L step 2L) { + if (t > limit) { + return true + } + if (candidate % t == 0L) { + return false + } + } + } +} + +fun perrinprime(n: Long): ArrayList<Long> { + var ot = mutableSetOf<Long>() + var seq = mutableListOf(3L, 0L, 2L) + while (true) { + val nv = seq[0] + seq[1] + seq.removeAt(0) + seq.add(nv) + if (isprime(nv)) { + ot.add(nv) + if (ot.size >= n) { + break; + } + } + } + var o = ArrayList(ot) + o.sort() + return o +} + +fun main() { + if (perrinprime(13) == listOf(2, 3, 5, 7, 17, 29, 277, 367, 853, + 14197, 43721, 1442968193, + 792606555396977)) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-168/roger-bell-west/kotlin/ch-2.kt b/challenge-168/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..7083d8f76f --- /dev/null +++ b/challenge-168/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,94 @@ +import kotlin.math.* + +fun genprimes(mx: Int): ArrayList<Int> { + var primesh=mutableSetOf<Int>() + for (i in 2..3) { + primesh.add(i) + } + for (i in 6..mx+1 step 6) { + for (j in i-1..i+1 step 2) { + if (j <= mx) { + primesh.add(j) + } + } + } + var q=ArrayDeque(listOf(2,3,5,7)) + var p=q.removeFirst() + val mr=sqrt(mx.toDouble()).toInt() + while (p <= mr) { + if (primesh.contains(p)) { + for (i in p*p..mx step p) { + primesh.remove(i) + } + } + if (q.size < 2) { + q.add(q.last()+4) + q.add(q.last()+2) + } + p=q.removeFirst() + } + var primes=ArrayList(primesh.distinct()) + primes.sort() + return primes +} + +fun primefactor(n: Int): Map<Int,Int> { + var f=mutableMapOf<Int,Int>() + var m=n + for (p in genprimes(sqrt(m.toDouble()).toInt())) { + while (m % p == 0) { + m /= p + if (f.containsKey(p)) { |
