44 * @see {@link https://projecteuler.net/problem=26 }
55 *
66 * Find the value of denominator < 1000 for which 1/denominator contains the longest recurring cycle in its decimal fraction part.
7- *
8- * A unit fraction (1/denominator) either terminates or repeats. We need to determine the length of the repeating sequence (cycle)
9- * for each fraction where the denominator is between 2 and 999, and find the denominator that produces the longest cycle.
107 */
118
129/**
@@ -23,49 +20,37 @@ function findLongestRecurringCycle(limit) {
2320 * @returns {number } The length of the recurring cycle in the decimal part of 1/denominator.
2421 */
2522 function getRecurringCycleLength ( denominator ) {
26- // A map to store the position of each remainder encountered during division
2723 const remainderPositions = new Map ( )
28- let numerator = 1 // We start with 1 as the numerator (as we're computing 1/denominator)
29- let position = 0 // This tracks the position of each digit in the decimal sequence
24+ let numerator = 1
25+ let position = 0
3026
31- // Continue until the remainder becomes 0 (terminating decimal) or a cycle is found
3227 while ( numerator !== 0 ) {
33- // If the remainder has been seen before, we've found the start of the cycle
3428 if ( remainderPositions . has ( numerator ) ) {
35- // The length of the cycle is the current position minus the position when the remainder first appeared
3629 return position - remainderPositions . get ( numerator )
3730 }
3831
39- // Record the position of this remainder
4032 remainderPositions . set ( numerator , position )
4133
42- // Multiply numerator by 10 to simulate long division and get the next digit
4334 numerator = ( numerator * 10 ) % denominator
44- position ++ // Move to the next digit position
35+ position ++
4536 }
4637
47- // If numerator becomes 0, it means the decimal terminates (no cycle)
4838 return 0
4939 }
5040
51- let maxCycleLength = 0 // Store the maximum cycle length found
52- let denominatorWithMaxCycle = 0 // Store the denominator corresponding to the longest cycle
41+ let maxCycleLength = 0
42+ let denominatorWithMaxCycle = 0
5343
54- // Iterate through each possible denominator from 2 up to (limit - 1)
5544 for ( let denominator = 2 ; denominator < limit ; denominator ++ ) {
56- // Calculate the cycle length for the current denominator
5745 const cycleLength = getRecurringCycleLength ( denominator )
5846
59- // Update the maximum length and the corresponding denominator if a longer cycle is found
6047 if ( cycleLength > maxCycleLength ) {
6148 maxCycleLength = cycleLength
6249 denominatorWithMaxCycle = denominator
6350 }
6451 }
6552
66- // Return the denominator that has the longest recurring cycle
6753 return denominatorWithMaxCycle
6854}
6955
70- // Exporting the main function for use in other modules
7156export { findLongestRecurringCycle }
0 commit comments