diff options
| -rw-r--r-- | challenge-167/julien-fiegehenn/perl/ch-1.pl | 80 | ||||
| -rw-r--r-- | challenge-167/julien-fiegehenn/typescript/ch-1.ts | 75 | ||||
| -rw-r--r-- | challenge-167/julien-fiegehenn/typescript/tsconfig.json | 7 |
3 files changed, 162 insertions, 0 deletions
diff --git a/challenge-167/julien-fiegehenn/perl/ch-1.pl b/challenge-167/julien-fiegehenn/perl/ch-1.pl new file mode 100644 index 0000000000..2274be6330 --- /dev/null +++ b/challenge-167/julien-fiegehenn/perl/ch-1.pl @@ -0,0 +1,80 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw(say); + +# Write a script to find out first 10 circular primes having at least 3 digits (base 10). +# A circular prime is a prime number with the property that the number generated at each +# intermediate step when cyclically permuting its (base 10) digits will also be prime. +# +# Output +# 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933 + +sub is_prime { + my $n = shift; + return 0 if $n < 2; + for my $i ( 2 .. int( $n**0.5 ) ) { + return 0 if $n % $i == 0; + } + return 1; +} + +sub is_circular_prime { + my $n = shift; + + return if $n =~ m/[^1379]/; + + my @rotations; + for ( 1 .. length $n ) { + return unless is_prime($n); + push @rotations, $n; + $n = chop($n) . $n; + } + return \@rotations; +} + +sub find_first_10_circular_primes { + my @primes; + my %seen; + my $i = 100; + while ( @primes < 10 ) { + $i++; + my $circular_prime = is_circular_prime($i); + next unless $circular_prime; + + push @primes, $i unless $seen{$i}; + $seen{$_}++ for @$circular_prime; + } + return @primes; +} + +say for find_first_10_circular_primes(); + +__END__ + +use Test::More; +use Test::Deep; + +ok is_prime(2), '2 is prime'; +ok is_prime(3), '3 is prime'; +ok is_prime(197), '197 is prime'; +ok !is_prime(4), '4 is not prime'; + +cmp_deeply( is_circular_prime(197), bag( 197, 971, 719 ), + 'is_circular_prime(197)' ); +cmp_deeply( is_circular_prime(199), bag( 199, 991, 919 ), + 'is_circular_prime(199)' ); +cmp_deeply( is_circular_prime(337), bag( 337, 733, 373 ), + 'is_circular_prime(337)' ); + +for my $i ( 4, 10, 55, 100 ) { + ok !is_circular_prime($i), "$i is not a circular prime"; +} + +cmp_deeply( + [ find_first_10_circular_primes() ], + [ 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933 ], + 'Find the first 10 circular primes' +); +done_testing; + diff --git a/challenge-167/julien-fiegehenn/typescript/ch-1.ts b/challenge-167/julien-fiegehenn/typescript/ch-1.ts new file mode 100644 index 0000000000..727dcd1ff8 --- /dev/null +++ b/challenge-167/julien-fiegehenn/typescript/ch-1.ts @@ -0,0 +1,75 @@ +// Write a script to find out first 10 circular primes having at least 3 digits (base 10). +// A circular prime is a prime number with the property that the number generated at each +// intermediate step when cyclically permuting its (base 10) digits will also be prime. +// +// Output +// 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933 + +// this script was written by github copilot. it's not very efficient, but it works. + +// a function to check if a number is prime +function isPrime(n: number): boolean { + if (n < 2) { + return false; + } + for (let i = 2; i < n; i++) { + if (n % i === 0) { + return false; + } + } + return true; +} + +type Result = boolean | number[]; +// a function to check if a number is circular prime +// returns false if it is not a circular prime +// returns an array of the circular permutations if it is a circular prime +function isCircularPrime(n: number): Result { + let circularPermutations: number[] = []; + let numberString: string = n.toString(); + let numberStringLength: number = numberString.length; + for (let i = 0; i < numberStringLength; i++) { + let circularPermutation: string = numberString.slice(i) + numberString.slice(0, i); + let circularPermutationNumber: number = parseInt(circularPermutation); + if (!isPrime(circularPermutationNumber)) { + return false; + } + circularPermutations.push(circularPermutationNumber); + } + return circularPermutations; +} +// a function to find the first 10 circular primes +// the prime has to have three digits +function findCircularPrimes(): number[] { + let circularPrimes: number[] = []; + let i = 0; + + // store a dictionary of all permutations of the numbers + let circularPrimesDictionary: { [key: number]: boolean } = {}; + while (circularPrimes.length < 10) { + // find out if the number is a circular prime and store the result + let result: Result = isCircularPrime(i); + // if the result is false, increment the number and continue + if (result === false) { + i++; + continue; + } + // if we have not seen the number before, add it to the circular primes array + if (circularPrimesDictionary[i] === undefined) { + // if the current number has at least 3 digits add it to the circular primes array + if (i.toString().length >= 3) { + circularPrimes.push(i); + } + } + + // store all the numbers from the result we have seen in the dictionary + for (let j = 0; j < (result as number[]).length; j++) { + circularPrimesDictionary[(result as number[])[j]] = true; + } + + i++; + } + return circularPrimes; +} + +console.log(findCircularPrimes());
\ No newline at end of file diff --git a/challenge-167/julien-fiegehenn/typescript/tsconfig.json b/challenge-167/julien-fiegehenn/typescript/tsconfig.json new file mode 100644 index 0000000000..3e3ab9fae8 --- /dev/null +++ b/challenge-167/julien-fiegehenn/typescript/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "target": "ES6", + "module": "CommonJS", + "lib": ["ES6", "DOM"] + } +}
\ No newline at end of file |
