diff options
| author | Julien Fiegehenn <simbabque@cpan.org> | 2022-06-02 20:35:03 +0100 |
|---|---|---|
| committer | Julien Fiegehenn <simbabque@cpan.org> | 2022-06-02 20:35:03 +0100 |
| commit | 9593ca2032db995850f7364173ba7e1f6b778d53 (patch) | |
| tree | d585aca334ed0d58fd97ab372e466229ac6df4b2 | |
| parent | 56184f6315e6fd3dfb4a11615812a47213e67b34 (diff) | |
| download | perlweeklychallenge-club-9593ca2032db995850f7364173ba7e1f6b778d53.tar.gz perlweeklychallenge-club-9593ca2032db995850f7364173ba7e1f6b778d53.tar.bz2 perlweeklychallenge-club-9593ca2032db995850f7364173ba7e1f6b778d53.zip | |
challenge 167, task 1 in typescript
This code was written with/by github copilot
| -rw-r--r-- | challenge-167/julien-fiegehenn/typescript/ch-1.ts | 75 | ||||
| -rw-r--r-- | challenge-167/julien-fiegehenn/typescript/tsconfig.json | 7 |
2 files changed, 82 insertions, 0 deletions
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 |
