diff --git a/challenges/euler/pnpm-lock.yaml b/challenges/euler/pnpm-lock.yaml index a77b2f4..073eb34 100644 --- a/challenges/euler/pnpm-lock.yaml +++ b/challenges/euler/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: 5.3 +lockfileVersion: 5.4 specifiers: '@swc/core': ^1.2.125 @@ -25,7 +25,7 @@ devDependencies: inquirer: 8.2.0 ms: 2.1.3 regenerator-runtime: 0.13.9 - ts-node: 10.4.0_626351e049b80b142acb2ce48a7f5656 + ts-node: 10.4.0_mjrvdycjxafrikwlftsiu72wky typescript: 4.4.4 packages: @@ -943,7 +943,7 @@ packages: os-tmpdir: 1.0.2 dev: true - /ts-node/10.4.0_626351e049b80b142acb2ce48a7f5656: + /ts-node/10.4.0_mjrvdycjxafrikwlftsiu72wky: resolution: { integrity: sha512-g0FlPvvCXSIO1JDF6S232P5jPYqBkRL9qly81ZgAOSU7rwI0stphCgd2kLiCrU9DjQCrJMWEqcNSjQL02s6d8A== diff --git a/challenges/euler/readme.md b/challenges/euler/readme.md index 6e963c6..f716051 100644 --- a/challenges/euler/readme.md +++ b/challenges/euler/readme.md @@ -37,7 +37,7 @@ The source code can be found in the [src](src) directory. My thoughts about some - [x] [23 - Non-abundant sums](src/23%20-%20Non-abundant%20sums.ts) - [x] [24 - Lexicographic permutations](src/24%20-%20Lexicographic%20permutations.ts) - [x] [25 - 1000-digit Fibonacci number](src/25%20-%201000-digit%20Fibonacci%20number.ts) -- [ ] 26 - Reciprocal cycles +- [x] [26 - Reciprocal cycles](src/26%20-%20Reciprocal%20cycles.ts) - [ ] 27 - Quadratic primes - [x] [28 - Number spiral diagonals](src/28%20-%20Number%20spiral%20diagonals.ts) - [ ] 29 - Distinct powers diff --git a/challenges/euler/src/26 - Reciprocal cycles.ts b/challenges/euler/src/26 - Reciprocal cycles.ts new file mode 100644 index 0000000..4681c46 --- /dev/null +++ b/challenges/euler/src/26 - Reciprocal cycles.ts @@ -0,0 +1,54 @@ +// A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: +// +// 1/2= 0.5 +// 1/3= 0.(3) +// 1/4= 0.25 +// 1/5= 0.2 +// 1/6= 0.1(6) +// 1/7= 0.(142857) +// 1/8= 0.125 +// 1/9= 0.(1) +// 1/10= 0.1 +// +// Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. +// Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part. +export = {}; + +const cycleLength = (denominator: number, numerator: number = 1) => { + let dividend = numerator; + let position = 1; + let lastPosition: { [number: number]: number } = {}; + + while (true) { + const remainder = dividend % denominator; + + // If the remainder is zero, there is no recurring cycle + if (remainder === 0) return 0; + + // If the remainder has been seen before, return. + if (lastPosition.hasOwnProperty(remainder)) { + return position - lastPosition[remainder]; + } + + // Move onto the next digit + lastPosition[remainder] = position; + position++; + dividend = remainder * 10; + } +}; + +let longestLength = 0; +let correspondingNumber = null; + +for (let i = 1; i < 1000; i++) { + const length = cycleLength(i); + + if (length > longestLength) { + longestLength = length; + correspondingNumber = i; + } +} + +console.log( + `The number with the longest recurring cycle is ${correspondingNumber} with a length of ${longestLength}.` +);