{ "name": "Project Euler", "order": 6, "time": "", "helpRoom": "HelpJavaScript", "challenges": [ { "id": "5900f36e1000cf542c50fe80", "challengeType": 5, "title": "Problem 1: Multiples of 3 and 5", "tests": [ { "text": "multiplesOf3and5(1000) should return 233168.", "testString": "assert.strictEqual(multiplesOf3and5(1000), 233168, 'multiplesOf3and5(1000) should return 233168.');" }, { "text": "multiplesOf3and5(49) should return 543.", "testString": "assert.strictEqual(multiplesOf3and5(49), 543, 'multiplesOf3and5(49) should return 543.');" }, { "text": "multiplesOf3and5(19564) should return 89301183.", "testString": "assert.strictEqual(multiplesOf3and5(19564), 89301183, 'multiplesOf3and5(19564) should return 89301183.');" }, { "text": "Your function is not returning the correct result using our tests values.", "testString": "assert.strictEqual(multiplesOf3and5(8456), 16687353, 'Your function is not returning the correct result using our tests values.');" } ], "solutions": [ "const multiplesOf3and5 = (number) => {\n var total = 0;\n\n for(var i = 0; i < number; i++) {\n if(i % 3 == 0 || i % 5 == 0) {\n total += i;\n }\n }\n return total;\n};" ], "translations": {}, "description": [ "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.", "Find the sum of all the multiples of 3 or 5 below the provided parameter value number." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function multiplesOf3and5(number) {", " // Good luck!", " return true;", "}", "", "multiplesOf3and5(1000);" ], "head": [], "tail": [] } } }, { "id": "5900f36e1000cf542c50fe81", "challengeType": 5, "title": "Problem 2: Even Fibonacci Numbers", "tests": [ { "text": "fiboEvenSum(10) should return 188.", "testString": "assert.strictEqual(fiboEvenSum(10), 188, 'fiboEvenSum(10) should return 188.');" }, { "text": "fiboEvenSum(23) should return 60696.", "testString": "assert.strictEqual(fiboEvenSum(23), 60696, 'fiboEvenSum(23) should return 60696.');" }, { "text": "fiboEvenSum(43) should return 1485607536.", "testString": "assert.strictEqual(fiboEvenSum(43), 1485607536, 'fiboEvenSum(43) should return 1485607536.');" }, { "text": "Your function is not returning the correct result using our tests values.", "testString": "assert.strictEqual(fiboEvenSum(18), 3382, 'Your function is not returning the correct result using our tests values.');" }, { "text": "Your function should return an even value.", "testString": "assert.equal(fiboEvenSum(31) % 2 === 0, true, 'Your function should return an even value.');" } ], "solutions": [ "const fiboEvenSum = (number) => {\n let temp, sum = 0, a = 0, b = 1;\n while (number >= 0) {\n temp = a;\n a = b;\n b += temp;\n number --;\n if ((b % 2) === 0) {\n sum += b;\n }\n }\n\n return sum;\n}" ], "translations": {}, "description": [ "Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:", "
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
", "By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function fiboEvenSum(number) {", " // You can do it!", " return true;", "}", "", "fiboEvenSum(10);" ], "head": [], "tail": [] } } }, { "id": "5900f36f1000cf542c50fe82", "challengeType": 5, "title": "Problem 3: Largest prime factor", "tests": [ { "text": "largestPrimeFactor(2) should return 2.", "testString": "assert.strictEqual(largestPrimeFactor(2), 2, 'largestPrimeFactor(2) should return 2.');" }, { "text": "largestPrimeFactor(3) should return 3.", "testString": "assert.strictEqual(largestPrimeFactor(3), 3, 'largestPrimeFactor(3) should return 3.');" }, { "text": "largestPrimeFactor(5) should return 5.", "testString": "assert.strictEqual(largestPrimeFactor(5), 5, 'largestPrimeFactor(5) should return 5.');" }, { "text": "largestPrimeFactor(7) should return 7.", "testString": "assert.strictEqual(largestPrimeFactor(7), 7, 'largestPrimeFactor(7) should return 7.');" }, { "text": "largestPrimeFactor(13195) should return 29.", "testString": "assert.strictEqual(largestPrimeFactor(13195), 29, 'largestPrimeFactor(13195) should return 29.');" }, { "text": "largestPrimeFactor(600851475143) should return 6857.", "testString": "assert.strictEqual(largestPrimeFactor(600851475143), 6857, 'largestPrimeFactor(600851475143) should return 6857.');" } ], "solutions": [ "const largestPrimeFactor = (number)=>{\n let largestFactor = number;\n for(let i = 2;ilargestPalindromeProduct(2) should return 9009.", "testString": "assert.strictEqual(largestPalindromeProduct(2), 9009, 'largestPalindromeProduct(2) should return 9009.');" }, { "text": "largestPalindromeProduct(3) should return 906609.", "testString": "assert.strictEqual(largestPalindromeProduct(3), 906609, 'largestPalindromeProduct(3) should return 906609.');" } ], "solutions": [ "const largestPalindromeProduct = (digit)=>{\n let start = 1;\n let end = Number(`1e${digit}`) - 1;\n let palindrome = [];\n for(let i=start;i<=end;i++){\n for(let j=start;j<=end;j++){\n let product = i*j;\n let palindromeRegex = /\\b(\\d)(\\d?)(\\d?).?\\3\\2\\1\\b/gi;\n palindromeRegex.test(product) && palindrome.push(product);\n }\n }\n return Math.max(...palindrome);\n}" ], "translations": {}, "description": [ "A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.", "Find the largest palindrome made from the product of two 3-digit numbers." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function largestPalindromeProduct(digit) {", " // Good luck!", " return true;", "}", "", "largestPalindromeProduct(3);" ], "head": [], "tail": [] } } }, { "id": "5900f3711000cf542c50fe84", "challengeType": 5, "title": "Problem 5: Smallest multiple", "tests": [ { "text": "smallestMult(5) should return 60.", "testString": "assert.strictEqual(smallestMult(5), 60, 'smallestMult(5) should return 60.');" }, { "text": "smallestMult(10) should return 2520.", "testString": "assert.strictEqual(smallestMult(10), 2520, 'smallestMult(10) should return 2520.');" }, { "text": "smallestMult(20) should return 232792560.", "testString": "assert.strictEqual(smallestMult(20), 232792560, 'smallestMult(20) should return 232792560.');" } ], "solutions": [ "function smallestMult(n){\n function gcd(a, b) {\n return b === 0 ? a : gcd(b, a%b); // Euclidean algorithm\n }\n\n function lcm(a, b) {\n return a * b / gcd(a, b);\n }\n var result = 1;\n for(var i = 2; i <= n; i++) {\n result = lcm(result, i);\n }\n return result;\n}" ], "translations": {}, "description": [ "2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.", "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function smallestMult(n) {", " // Good luck!", " return true;", "}", "", "smallestMult(20);" ], "head": [], "tail": [] } } }, { "id": "5900f3721000cf542c50fe85", "challengeType": 5, "title": "Problem 6: Sum square difference", "tests": [ { "text": "sumSquareDifference(10) should return 2640.", "testString": "assert.strictEqual(sumSquareDifference(10), 2640, 'sumSquareDifference(10) should return 2640.');" }, { "text": "sumSquareDifference(20) should return 41230.", "testString": "assert.strictEqual(sumSquareDifference(20), 41230, 'sumSquareDifference(20) should return 41230.');" }, { "text": "sumSquareDifference(100) should return 25164150.", "testString": "assert.strictEqual(sumSquareDifference(100), 25164150, 'sumSquareDifference(100) should return 25164150.');" } ], "solutions": [ "const sumSquareDifference = (number)=>{\n let squareOfSum = Math.pow(sumOfArithmeticSeries(1,1,number),2);\n let sumOfSquare = sumOfSquareOfNumbers(number);\n return squareOfSum - sumOfSquare;\n}\n\nfunction sumOfArithmeticSeries(a,d,n){\n return (n/2)*(2*a+(n-1)*d);\n}\n\nfunction sumOfSquareOfNumbers(n){\n return (n*(n+1)*(2*n+1))/6;\n}" ], "translations": {}, "description": [ "The sum of the squares of the first ten natural numbers is,", "
12 + 22 + ... + 102 = 385
", "The square of the sum of the first ten natural numbers is,", "
(1 + 2 + ... + 10)2 = 552 = 3025
", "Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.", "Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function sumSquareDifference(number) {", " // Good luck!", " return true;", "}", "", "sumSquareDifference(100);" ], "head": [], "tail": [] } } }, { "id": "5900f3731000cf542c50fe86", "challengeType": 5, "title": "Problem 7: 10001st prime", "tests": [ { "text": "nthPrime(6) should return 13.", "testString": "assert.strictEqual(nthPrime(6), 13, 'nthPrime(6) should return 13.');" }, { "text": "nthPrime(10) should return 29.", "testString": "assert.strictEqual(nthPrime(10), 29, 'nthPrime(10) should return 29.');" }, { "text": "nthPrime(100) should return 541.", "testString": "assert.strictEqual(nthPrime(100), 541, 'nthPrime(100) should return 541.');" }, { "text": "nthPrime(1000) should return 7919.", "testString": "assert.strictEqual(nthPrime(1000), 7919, 'nthPrime(1000) should return 7919.');" }, { "text": "nthPrime(10001) should return 104743.", "testString": "assert.strictEqual(nthPrime(10001), 104743, 'nthPrime(10001) should return 104743.');" } ], "solutions": [ "const nthPrime = (number)=>{\n let pN = 2;\n let step = 0;\n while (steplargestProductinaSeries(4) should return 5832.", "testString": "assert.strictEqual(largestProductinaSeries(4), 5832, 'largestProductinaSeries(4) should return 5832.');" }, { "text": "largestProductinaSeries(13) should return 23514624000.", "testString": "assert.strictEqual(largestProductinaSeries(13), 23514624000, 'largestProductinaSeries(13) should return 23514624000.');" } ], "solutions": [ "const largestProductinaSeries = (number)=>{\n let thousandDigits = [7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7,5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2,8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4,8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1,1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6,1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0];\n let numberOfDigits = thousandDigits.length;\n let currentIndex = 0;\n let productOfAdjDigits = [];\n\n while(currentIndex<=(numberOfDigits-number)){\n let currentAdj = thousandDigits.slice(currentIndex,currentIndex+number);\n let isAdjDigits = false;\n\n productOfAdjDigits.push(currentAdj.reduce((prev,cur)=>{\n return prev*cur;\n }));\n\n currentIndex++;\n }\n\n return Math.max(...productOfAdjDigits);\n}" ], "translations": {}, "description": [ "The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.", "", "
73167176531330624919225119674426574742355349194934
", "
96983520312774506326239578318016984801869478851843
", "
85861560789112949495459501737958331952853208805511
", "
12540698747158523863050715693290963295227443043557
", "
66896648950445244523161731856403098711121722383113
", "
62229893423380308135336276614282806444486645238749
", "
30358907296290491560440772390713810515859307960866
", "
70172427121883998797908792274921901699720888093776
", "
65727333001053367881220235421809751254540594752243
", "
52584907711670556013604839586446706324415722155397
", "
53697817977846174064955149290862569321978468622482
", "
83972241375657056057490261407972968652414535100474
", "
82166370484403199890008895243450658541227588666881
", "
16427171479924442928230863465674813919123162824586
", "
17866458359124566529476545682848912883142607690042
", "
24219022671055626321111109370544217506941658960408
", "
07198403850962455444362981230987879927244284909188
", "
84580156166097919133875499200524063689912560717606
", "
05886116467109405077541002256983155200055935729725
", "
71636269561882670428252483600823257530420752963450
", "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function largestProductinaSeries(number) {", " // Good luck!", " let thousandDigits = [7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7,5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2,8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4,8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1,1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6,1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0];", " return true;", "}", "", "largestProductinaSeries(13);" ], "head": [], "tail": [] } } }, { "id": "5900f3761000cf542c50fe88", "challengeType": 5, "title": "Problem 9: Special Pythagorean triplet", "tests": [ { "text": "specialPythagoreanTriplet(1000) should return 31875000.", "testString": "assert.strictEqual(specialPythagoreanTriplet(1000), 31875000, 'specialPythagoreanTriplet(1000) should return 31875000.');" }, { "text": "specialPythagoreanTriplet(24) should return 480.", "testString": "assert.strictEqual(specialPythagoreanTriplet(24), 480, 'specialPythagoreanTriplet(24) should return 480.');" }, { "text": "specialPythagoreanTriplet(120) should return 49920.", "testString": "assert.strictEqual(specialPythagoreanTriplet(120), 49920, 'specialPythagoreanTriplet(120) should return 49920.');" } ], "solutions": [ "const specialPythagoreanTriplet = (n)=>{\n let sumOfabc = n;\n let a,b,c;\n for(a = 1; a<=sumOfabc/3; a++){\n for(b = a+1; b<=sumOfabc/2; b++){\n c = Math.sqrt(a*a+b*b);\n if((a+b+c) == sumOfabc){\n return a*b*c;\n }\n }\n }\n}" ], "translations": {}, "description": [ "A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,", "
a2 + b2 = c2
", "For example, 32 + 42 = 9 + 16 = 25 = 52.", "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function specialPythagoreanTriplet(n) {", " let sumOfabc = n;", " // Good luck!", " return true;", "}", "", "specialPythagoreanTriplet(1000);" ], "head": [], "tail": [] } } }, { "id": "5900f3761000cf542c50fe89", "challengeType": 5, "title": "Problem 10: Summation of primes", "tests": [ { "text": "primeSummation(17) should return 41.", "testString": "assert.strictEqual(primeSummation(17), 41, 'primeSummation(17) should return 41.');" }, { "text": "primeSummation(2001) should return 277050.", "testString": "assert.strictEqual(primeSummation(2001), 277050, 'primeSummation(2001) should return 277050.');" }, { "text": "primeSummation(140759) should return 873608362.", "testString": "assert.strictEqual(primeSummation(140759), 873608362, 'primeSummation(140759) should return 873608362.');" }, { "text": "primeSummation(2000000) should return 142913828922.", "testString": "assert.strictEqual(primeSummation(2000000), 142913828922, 'primeSummation(2000000) should return 142913828922.');" } ], "solutions": [ "//noprotect\nfunction primeSummation(n) {\n // Initialise an array containing only prime numbers\n let primes = [2];\n let result = 2;\n\n function isPrime(y, primes) {\n // Find sqrt(y)\n const sqrt = Math.floor(Math.sqrt(y));\n\n // Divide y by each applicable prime, return false if any of them divide y\n for (let i = 0; i < primes.length && primes[i] <= sqrt; i++) {\n if (y % primes[i] === 0) {\n return false;\n }\n }\n\n // At this point x must be prime\n return true;\n }\n\n // For every odd integer, add it to the array if it is prime\n for (let x = 3; x < n; x += 2) {\n if (isPrime(x, primes)) {\n if (x > n) {\n return result;\n } else {\n result += x;\n primes.push(x);\n }\n }\n }\n\n return result;\n}" ], "translations": {}, "description": [ "The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.", "Find the sum of all the primes below n." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function primeSummation(n) {", " // Good luck!", " return true;", "}", "", "primeSummation(2000000);" ], "head": [], "tail": [] } } }, { "id": "5900f3781000cf542c50fe8a", "challengeType": 5, "title": "Problem 11: Largest product in a grid", "tests": [ { "text": "largestGridProduct(grid) should return 70600674.", "testString": "assert.strictEqual(largestGridProduct(grid), 70600674, 'largestGridProduct(grid) should return 70600674.');" }, { "text": "largestGridProduct(testGrid) should return 14169081.", "testString": "assert.strictEqual(largestGridProduct(testGrid), 14169081, 'largestGridProduct(testGrid) should return 14169081.');" } ], "solutions": [ "function largestGridProduct(arr) {\n let maxProduct = 0;\n let currProduct = 0;\n\n function maxProductChecker(n) {\n if (n > maxProduct) {\n return maxProduct = n;\n }\n }\n\n // loop rows\n for (let r = 0; r < arr.length; r++) {\n // loop columns\n for (let c = 0; c < arr[r].length; c++) {\n const limit = arr[r].length - 3;\n\n // check horizontal\n if (c < limit) {\n currProduct = arr[r][c] * arr[r][c + 1] * arr[r][c + 2] * arr[r][c + 3];\n maxProductChecker(currProduct);\n }\n\n // check vertical\n if (r < limit) {\n currProduct = arr[r][c] * arr[r + 1][c] * arr[r + 2][c] * arr[r + 3][c];\n maxProductChecker(currProduct);\n }\n\n // check diagonal [\\]\n if (c < limit && r < limit) {\n currProduct = arr[r][c] * arr[r + 1][c + 1] * arr[r + 2][c + 2] * arr[r + 3][c + 3];\n maxProductChecker(currProduct);\n }\n\n // check diagonal [/]\n if (c > 3 && r < limit) {\n currProduct = arr[r][c] * arr[r + 1][c - 1] * arr[r + 2][c - 2] * arr[r + 3][c - 3];\n maxProductChecker(currProduct);\n }\n }\n }\n\n return maxProduct;\n}\n\n const grid = [ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],\n [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],\n [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],\n [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],\n [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],\n [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],\n [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],\n [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],\n [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],\n [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],\n [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],\n [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],\n [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],\n [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],\n [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],\n [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],\n [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],\n [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],\n [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],\n [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]\n];\n\nconst testGrid = [\n [40, 17, 81, 18, 57],\n [74, 4, 36, 16, 29],\n [36, 42, 69, 73, 45],\n [51, 54, 69, 16, 92],\n [7, 97, 57, 32, 16]\n];" ], "translations": {}, "description": [ "In the 20×20 grid below, four numbers along a diagonal line have been marked in red.", "", "
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
", "
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
", "
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
", "
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
", "
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
", "
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
", "
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
", "
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
", "
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
", "
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
", "
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
", "
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
", "
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
", "
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
", "
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
", "
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
", "
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
", "
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
", "
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
", "
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
", "", "The product of these numbers is 26 × 63 × 78 × 14 = 1788696.", "What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function largestGridProduct(arr) {", " // Good luck!", " return arr;", "}", "", "// Only change code above this line", "const grid = [", " [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],", " [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],", " [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],", " [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],", " [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],", " [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],", " [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],", " [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],", " [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],", " [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],", " [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],", " [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],", " [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],", " [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],", " [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],", " [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],", " [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],", " [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],", " [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],", " [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]", "];", "", "const testGrid = [", " [40, 17, 81, 18, 57],", " [74, 4, 36, 16, 29],", " [36, 42, 69, 73, 45],", " [51, 54, 69, 16, 92],", " [7, 97, 57, 32, 16]", "];", "", "largestGridProduct(testGrid);" ], "head": [], "tail": [] } } }, { "id": "5900f3781000cf542c50fe8b", "challengeType": 5, "title": "Problem 12: Highly divisible triangular number", "tests": [ { "text": "divisibleTriangleNumber(5) should return 28.", "testString": "assert.strictEqual(divisibleTriangleNumber(5), 28, 'divisibleTriangleNumber(5) should return 28.');" }, { "text": "divisibleTriangleNumber(23) should return 630.", "testString": "assert.strictEqual(divisibleTriangleNumber(23), 630, 'divisibleTriangleNumber(23) should return 630.');" }, { "text": "divisibleTriangleNumber() should return 76576500.", "testString": "assert.strictEqual(divisibleTriangleNumber(500), 76576500, 'divisibleTriangleNumber() should return 76576500.');" } ], "solutions": [ "function divisibleTriangleNumber(n) {\n let counter = 1;\n let triangleNumber = counter++;\n\n function getFactors(num) {\n let factors = [];\n\n let possibleFactor = 1;\n let sqrt = Math.sqrt(num);\n\n while (possibleFactor <= sqrt) {\n if (num % possibleFactor == 0) {\n factors.push(possibleFactor);\n var otherPossibleFactor = num / possibleFactor;\n if (otherPossibleFactor > possibleFactor) {\n factors.push(otherPossibleFactor);\n }\n }\n possibleFactor++;\n }\n\n return factors;\n }\n\n while (getFactors(triangleNumber).length < n) {\n triangleNumber += counter++;\n }\n console.log(triangleNumber)\n return triangleNumber;\n}" ], "translations": {}, "description": [ "The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:", "
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
", "Let us list the factors of the first seven triangle numbers:", "
1: 1
", "
3: 1, 3
", "
6: 1, 2, 3, 6
", "
10: 1, 2, 5, 10
", "
15: 1, 3, 5, 15
", "
21: 1, 3, 7, 21
", "
28: 1, 2, 4, 7, 14, 28
", "We can see that 28 is the first triangle number to have over five divisors.", "What is the value of the first triangle number to have over five hundred divisors?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function divisibleTriangleNumber(n) {", " // Good luck!", " return true;", "}", "", "divisibleTriangleNumber(500);" ], "head": [], "tail": [] } } }, { "id": "5900f37a1000cf542c50fe8c", "challengeType": 5, "title": "Problem 13: Large sum", "tests": [ { "text": "largeSum(testNums) should return 8348422521.", "testString": "assert.strictEqual(largeSum(testNums), 8348422521, 'largeSum(testNums) should return 8348422521.');" }, { "text": "largeSum(fiftyDigitNums) should return 5537376230.", "testString": "assert.strictEqual(largeSum(fiftyDigitNums), 5537376230, 'largeSum(fiftyDigitNums) should return 5537376230.');" } ], "solutions": [ "function largeSum(arr) {\n\n let sum = 0;\n\n arr.forEach(function(num) {\n sum += parseInt(num, 10);\n });\n\n sum = sum.toString(10);\n\n sum = sum.substr(0, 1) + sum.substr(2);\n\n let firstTen = sum.slice(0, 10);\n return parseInt(firstTen, 10);\n}" ], "translations": {}, "description": [ "Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.", "", "37107287533902102798797998220837590246510135740250", "46376937677490009712648124896970078050417018260538", "74324986199524741059474233309513058123726617309629", "91942213363574161572522430563301811072406154908250", "23067588207539346171171980310421047513778063246676", "89261670696623633820136378418383684178734361726757", "28112879812849979408065481931592621691275889832738", "44274228917432520321923589422876796487670272189318", "47451445736001306439091167216856844588711603153276", "70386486105843025439939619828917593665686757934951", "62176457141856560629502157223196586755079324193331", "64906352462741904929101432445813822663347944758178", "92575867718337217661963751590579239728245598838407", "58203565325359399008402633568948830189458628227828", "80181199384826282014278194139940567587151170094390", "35398664372827112653829987240784473053190104293586", "86515506006295864861532075273371959191420517255829", "71693888707715466499115593487603532921714970056938", "54370070576826684624621495650076471787294438377604", "53282654108756828443191190634694037855217779295145", "36123272525000296071075082563815656710885258350721", "45876576172410976447339110607218265236877223636045", "17423706905851860660448207621209813287860733969412", "81142660418086830619328460811191061556940512689692", "51934325451728388641918047049293215058642563049483", "62467221648435076201727918039944693004732956340691", "15732444386908125794514089057706229429197107928209", "55037687525678773091862540744969844508330393682126", "18336384825330154686196124348767681297534375946515", "80386287592878490201521685554828717201219257766954", "78182833757993103614740356856449095527097864797581", "16726320100436897842553539920931837441497806860984", "48403098129077791799088218795327364475675590848030", "87086987551392711854517078544161852424320693150332", "59959406895756536782107074926966537676326235447210", "69793950679652694742597709739166693763042633987085", "41052684708299085211399427365734116182760315001271", "65378607361501080857009149939512557028198746004375", "35829035317434717326932123578154982629742552737307", "94953759765105305946966067683156574377167401875275", "88902802571733229619176668713819931811048770190271", "25267680276078003013678680992525463401061632866526", "36270218540497705585629946580636237993140746255962", "24074486908231174977792365466257246923322810917141", "91430288197103288597806669760892938638285025333403", "34413065578016127815921815005561868836468420090470", "23053081172816430487623791969842487255036638784583", "11487696932154902810424020138335124462181441773470", "63783299490636259666498587618221225225512486764533", "67720186971698544312419572409913959008952310058822", "95548255300263520781532296796249481641953868218774", "76085327132285723110424803456124867697064507995236", "37774242535411291684276865538926205024910326572967", "23701913275725675285653248258265463092207058596522", "29798860272258331913126375147341994889534765745501", "18495701454879288984856827726077713721403798879715", "38298203783031473527721580348144513491373226651381", "34829543829199918180278916522431027392251122869539", "40957953066405232632538044100059654939159879593635", "29746152185502371307642255121183693803580388584903", "41698116222072977186158236678424689157993532961922", "62467957194401269043877107275048102390895523597457", "23189706772547915061505504953922979530901129967519", "86188088225875314529584099251203829009407770775672", "11306739708304724483816533873502340845647058077308", "82959174767140363198008187129011875491310547126581", "97623331044818386269515456334926366572897563400500", "42846280183517070527831839425882145521227251250327", "55121603546981200581762165212827652751691296897789", "32238195734329339946437501907836945765883352399886", "75506164965184775180738168837861091527357929701337", "62177842752192623401942399639168044983993173312731", "32924185707147349566916674687634660915035914677504", "99518671430235219628894890102423325116913619626622", "73267460800591547471830798392868535206946944540724", "76841822524674417161514036427982273348055556214818", "97142617910342598647204516893989422179826088076852", "87783646182799346313767754307809363333018982642090", "10848802521674670883215120185883543223812876952786", "71329612474782464538636993009049310363619763878039", "62184073572399794223406235393808339651327408011116", "66627891981488087797941876876144230030984490851411", "60661826293682836764744779239180335110989069790714", "85786944089552990653640447425576083659976645795096", "66024396409905389607120198219976047599490197230297", "64913982680032973156037120041377903785566085089252", "16730939319872750275468906903707539413042652315011", "94809377245048795150954100921645863754710598436791", "78639167021187492431995700641917969777599028300699", "15368713711936614952811305876380278410754449733078", "40789923115535562561142322423255033685442488917353", "44889911501440648020369068063960672322193204149535", "41503128880339536053299340368006977710650566631954", "81234880673210146739058568557934581403627822703280", "82616570773948327592232845941706525094512325230608", "22918802058777319719839450180888072429661980811197", "77158542502016545090413245809786882778948721859617", "72107838435069186155435662884062257473692284509516", "20849603980134001723930671666823555245252804609722", "53503534226472524250874054075591789781264330331690" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function largeSum(arr) {", " // Good luck!", " return true;", "}", "", "// only change code above this line", "", "const testNums = [", " '37107287533902102798797998220837590246510135740250',", " '46376937677490009712648124896970078050417018260538'", "];", "", "largeSum(testNums);" ], "head": [ "const fiftyDigitNums = [", " '37107287533902102798797998220837590246510135740250',", " '46376937677490009712648124896970078050417018260538',", " '74324986199524741059474233309513058123726617309629',", " '91942213363574161572522430563301811072406154908250',", " '23067588207539346171171980310421047513778063246676',", " '89261670696623633820136378418383684178734361726757',", " '28112879812849979408065481931592621691275889832738',", " '44274228917432520321923589422876796487670272189318',", " '47451445736001306439091167216856844588711603153276',", " '70386486105843025439939619828917593665686757934951',", " '62176457141856560629502157223196586755079324193331',", " '64906352462741904929101432445813822663347944758178',", " '92575867718337217661963751590579239728245598838407',", " '58203565325359399008402633568948830189458628227828',", " '80181199384826282014278194139940567587151170094390',", " '35398664372827112653829987240784473053190104293586',", " '86515506006295864861532075273371959191420517255829',", " '71693888707715466499115593487603532921714970056938',", " '54370070576826684624621495650076471787294438377604',", " '53282654108756828443191190634694037855217779295145',", " '36123272525000296071075082563815656710885258350721',", " '45876576172410976447339110607218265236877223636045',", " '17423706905851860660448207621209813287860733969412',", " '81142660418086830619328460811191061556940512689692',", " '51934325451728388641918047049293215058642563049483',", " '62467221648435076201727918039944693004732956340691',", " '15732444386908125794514089057706229429197107928209',", " '55037687525678773091862540744969844508330393682126',", " '18336384825330154686196124348767681297534375946515',", " '80386287592878490201521685554828717201219257766954',", " '78182833757993103614740356856449095527097864797581',", " '16726320100436897842553539920931837441497806860984',", " '48403098129077791799088218795327364475675590848030',", " '87086987551392711854517078544161852424320693150332',", " '59959406895756536782107074926966537676326235447210',", " '69793950679652694742597709739166693763042633987085',", " '41052684708299085211399427365734116182760315001271',", " '65378607361501080857009149939512557028198746004375',", " '35829035317434717326932123578154982629742552737307',", " '94953759765105305946966067683156574377167401875275',", " '88902802571733229619176668713819931811048770190271',", " '25267680276078003013678680992525463401061632866526',", " '36270218540497705585629946580636237993140746255962',", " '24074486908231174977792365466257246923322810917141',", " '91430288197103288597806669760892938638285025333403',", " '34413065578016127815921815005561868836468420090470',", " '23053081172816430487623791969842487255036638784583',", " '11487696932154902810424020138335124462181441773470',", " '63783299490636259666498587618221225225512486764533',", " '67720186971698544312419572409913959008952310058822',", " '95548255300263520781532296796249481641953868218774',", " '76085327132285723110424803456124867697064507995236',", " '37774242535411291684276865538926205024910326572967',", " '23701913275725675285653248258265463092207058596522',", " '29798860272258331913126375147341994889534765745501',", " '18495701454879288984856827726077713721403798879715',", " '38298203783031473527721580348144513491373226651381',", " '34829543829199918180278916522431027392251122869539',", " '40957953066405232632538044100059654939159879593635',", " '29746152185502371307642255121183693803580388584903',", " '41698116222072977186158236678424689157993532961922',", " '62467957194401269043877107275048102390895523597457',", " '23189706772547915061505504953922979530901129967519',", " '86188088225875314529584099251203829009407770775672',", " '11306739708304724483816533873502340845647058077308',", " '82959174767140363198008187129011875491310547126581',", " '97623331044818386269515456334926366572897563400500',", " '42846280183517070527831839425882145521227251250327',", " '55121603546981200581762165212827652751691296897789',", " '32238195734329339946437501907836945765883352399886',", " '75506164965184775180738168837861091527357929701337',", " '62177842752192623401942399639168044983993173312731',", " '32924185707147349566916674687634660915035914677504',", " '99518671430235219628894890102423325116913619626622',", " '73267460800591547471830798392868535206946944540724',", " '76841822524674417161514036427982273348055556214818',", " '97142617910342598647204516893989422179826088076852',", " '87783646182799346313767754307809363333018982642090',", " '10848802521674670883215120185883543223812876952786',", " '71329612474782464538636993009049310363619763878039',", " '62184073572399794223406235393808339651327408011116',", " '66627891981488087797941876876144230030984490851411',", " '60661826293682836764744779239180335110989069790714',", " '85786944089552990653640447425576083659976645795096',", " '66024396409905389607120198219976047599490197230297',", " '64913982680032973156037120041377903785566085089252',", " '16730939319872750275468906903707539413042652315011',", " '94809377245048795150954100921645863754710598436791',", " '78639167021187492431995700641917969777599028300699',", " '15368713711936614952811305876380278410754449733078',", " '40789923115535562561142322423255033685442488917353',", " '44889911501440648020369068063960672322193204149535',", " '41503128880339536053299340368006977710650566631954',", " '81234880673210146739058568557934581403627822703280',", " '82616570773948327592232845941706525094512325230608',", " '22918802058777319719839450180888072429661980811197',", " '77158542502016545090413245809786882778948721859617',", " '72107838435069186155435662884062257473692284509516',", " '20849603980134001723930671666823555245252804609722',", " '53503534226472524250874054075591789781264330331690'", "];", "", "const testNums = [", " '37107287533902102798797998220837590246510135740250',", " '46376937677490009712648124896970078050417018260538'", "];" ], "tail": [] } } }, { "id": "5900f37a1000cf542c50fe8d", "challengeType": 5, "title": "Problem 14: Longest Collatz sequence", "tests": [ { "text": "longestCollatzSequence(14) should return 9.", "testString": "assert.strictEqual(longestCollatzSequence(14), 9, 'longestCollatzSequence(14) should return 9.');" }, { "text": "longestCollatzSequence(5847) should return 3711.", "testString": "assert.strictEqual(longestCollatzSequence(5847), 3711, 'longestCollatzSequence(5847) should return 3711.');" }, { "text": "longestCollatzSequence(1000000) should return 837799.", "testString": "assert.strictEqual(longestCollatzSequence(1000000), 837799, 'longestCollatzSequence(1000000) should return 837799.');" } ], "solutions": [ "function longestCollatzSequence(limit) {\n let longestSequenceLength = 0;\n let startingNum = 0;\n\n function sequenceLength(num) {\n let length = 1;\n\n while (num >= 1) {\n if (num === 1) { break;\n } else if (num % 2 === 0) {\n num = num / 2;\n length++;\n } else {\n num = num * 3 + 1;\n length++;\n }\n }\n return length;\n }\n\n for (let i = 2; i < limit; i++) {\n let currSequenceLength = sequenceLength(i);\n if (currSequenceLength > longestSequenceLength) {\n longestSequenceLength = currSequenceLength;\n startingNum = i;\n }\n }\n return startingNum;\n}" ], "translations": {}, "description": [ "The following iterative sequence is defined for the set of positive integers:", "
nn/2 (n is even)
", "
n → 3n + 1 (n is odd)
", "Using the rule above and starting with 13, we generate the following sequence:", "
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1
", "It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.", "Which starting number, under one million, produces the longest chain?", "NOTE: Once the chain starts the terms are allowed to go above one million." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function longestCollatzSequence(limit) {", " // Good luck!", " return true;", "}", "", "longestCollatzSequence(14);" ], "head": [], "tail": [] } } }, { "id": "5900f37b1000cf542c50fe8e", "challengeType": 5, "title": "Problem 15: Lattice paths", "tests": [ { "text": "latticePaths(4) should return 70.", "testString": "assert.strictEqual(latticePaths(4), 70, 'latticePaths(4) should return 70.');" }, { "text": "latticePaths(9) should return 48620.", "testString": "assert.strictEqual(latticePaths(9), 48620, 'latticePaths(9) should return 48620.');" }, { "text": "latticePaths(20) should return 137846528820.", "testString": "assert.strictEqual(latticePaths(20), 137846528820, 'latticePaths(20) should return 137846528820.');" } ], "solutions": [ "function latticePaths(gridSize) {\n let paths = 1;\n\n for (let i = 0; i < gridSize; i++) {\n paths *= (2 * gridSize) - i;\n paths /= i + 1;\n }\n return paths;\n}" ], "translations": {}, "description": [ "Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.", "", "\"a", "", "How many such routes are there through a 20×20 grid?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function latticePaths(gridSize) {", " // Good luck!", " return true;", "}", "", "latticePaths(4);" ], "head": [], "tail": [] } } }, { "id": "5900f37d1000cf542c50fe8f", "challengeType": 5, "title": "Problem 16: Power digit sum", "tests": [ { "text": "powerDigitSum(15) should return 26.", "testString": "assert.strictEqual(powerDigitSum(15), 26, 'powerDigitSum(15) should return 26.');" }, { "text": "powerDigitSum(128) should return 166.", "testString": "assert.strictEqual(powerDigitSum(128), 166, 'powerDigitSum(128) should return 166.');" }, { "text": "powerDigitSum(1000) should return 1366.", "testString": "assert.strictEqual(powerDigitSum(1000), 1366, 'powerDigitSum(1000) should return 1366.');" } ], "solutions": [ "function powerDigitSum(exponent) {\n const bigNum = [1];\n let sum = 0;\n\n for (let i = 1; i <= exponent; i++) {\n let count = bigNum.length + 1;\n let overflow = 0;\n for (let j = 0; j < count; j++) {\n let digit = bigNum[j] || 0;\n digit = 2 * digit + overflow;\n\n if (digit > 9) {\n digit -= 10;\n overflow = 1;\n } else {\n overflow = 0;\n }\n\n bigNum[j] = digit;\n }\n }\n\n bigNum.forEach(function(num) {\n return sum += num;\n });\n\n return sum;\n}" ], "translations": {}, "description": [ "215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.", "What is the sum of the digits of the number 21000?" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function powerDigitSum(exponent) {", " // Good luck!", " return true;", "}", "", "powerDigitSum(15);" ], "head": [], "tail": [] } } }, { "id": "5900f37d1000cf542c50fe90", "challengeType": 5, "title": "Problem 17: Number letter counts", "tests": [ { "text": "numberLetterCounts(5) should return 19.", "testString": "assert.strictEqual(numberLetterCounts(5), 19, 'numberLetterCounts(5) should return 19.');" }, { "text": "numberLetterCounts(150) should return 1903.", "testString": "assert.strictEqual(numberLetterCounts(150), 1903, 'numberLetterCounts(150) should return 1903.');" }, { "text": "numberLetterCounts(1000) should return 21124.", "testString": "assert.strictEqual(numberLetterCounts(1000), 21124, 'numberLetterCounts(1000) should return 21124.');" } ], "solutions": [ "function numberLetterCounts(limit) {\n const dictionary = {\n 0: '',\n 1: 'one',\n 2: 'two',\n 3: 'three',\n 4: 'four',\n 5: 'five',\n 6: 'six',\n 7: 'seven',\n 8: 'eight',\n 9: 'nine',\n 10: 'ten',\n 11: 'eleven',\n 12: 'twelve',\n 13: 'thirteen',\n 14: 'fourteen',\n 15: 'fifteen',\n 16: 'sixteen',\n 17: 'seventeen',\n 18: 'eighteen',\n 19: 'nineteen',\n 20: 'twenty',\n 30: 'thirty',\n 40: 'forty',\n 50: 'fifty',\n 60: 'sixty',\n 70: 'seventy',\n 80: 'eighty',\n 90: 'ninety',\n 1000: 'onethousand'\n };\n\n let numString = '';\n\n function convertToString(num) {\n // check dictionary for number\n if (dictionary[num]) {\n return dictionary[num];\n } else {\n const hundreds = Math.floor(num / 100);\n const tens = Math.floor((num / 10) % 10) * 10;\n const remainder = num % 10;\n\n let tempStr = '';\n\n if (hundreds === 0) {\n tempStr += dictionary[tens] + dictionary[remainder];\n } else {\n tempStr += dictionary[hundreds] + 'hundred';\n\n if (tens !== 0 || remainder !== 0) {\n tempStr += 'and';\n }\n\n if (tens < 20) {\n const lessThanTwenty = tens + remainder;\n tempStr += dictionary[lessThanTwenty];\n } else {\n tempStr += dictionary[tens] + dictionary[remainder];\n }\n }\n // console.log(num, hundreds, tens, remainder);\n return tempStr;\n }\n }\n\n for (let i = 1; i <= limit; i++) {\n numString += convertToString(i);\n }\n return numString.length;\n}" ], "translations": {}, "description": [ "If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.", "If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? ", "NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of \"and\" when writing out numbers is in compliance with British usage." ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function numberLetterCounts(limit) {", " // Good luck!", " return true;", "}", "", "numberLetterCounts(5);" ], "head": [], "tail": [] } } }, { "id": "5900f37e1000cf542c50fe91", "challengeType": 5, "title": "Problem 18: Maximum path sum I", "tests": [ { "text": "maximumPathSumI(testTriangle) should return 23.", "testString": "assert.strictEqual(maximumPathSumI(testTriangle), 23, 'maximumPathSumI(testTriangle) should return 23.');" }, { "text": "maximumPathSumI(numTriangle) should return 1074.", "testString": "assert.strictEqual(maximumPathSumI(numTriangle), 1074, 'maximumPathSumI(numTriangle) should return 1074.');" } ], "solutions": [ "const testTriangle = [[3, 0, 0, 0],\n [7, 4, 0, 0],\n [2, 4, 6, 0],\n [8, 5, 9, 3]];\n\nfunction maximumPathSumI(triangle) {\n let maxSum = triangle.slice();\n\n for (let i = triangle.length - 1; i > 0; i--) {\n let currentRow = maxSum[i];\n let previousRow = maxSum[i - 1];\n const temp = [];\n for (let j = 0; j < i; j++) {\n temp.push(Math.max((currentRow[j] + previousRow[j]), (currentRow[j + 1] + previousRow[j])));\n }\n maxSum[i - 1] = temp;\n maxSum.pop();\n }\n return maxSum[0][0];\n}" ], "translations": {}, "description": [ "By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.", "3
7 4
2 4 6
8 5 9 3
", "That is, 3 + 7 + 4 + 9 = 23.", "Find the maximum total from top to bottom of the triangle below:", "75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
19 01 23 75 03 34
88 02 77 73 07 63 67
99 65 04 28 06 16 70 92
41 41 26 56 83 40 80 70 33
41 48 72 33 47 32 37 16 94 29
53 71 44 65 25 43 91 52 97 51 14
70 11 33 28 77 73 17 78 39 68 17 57
91 71 52 38 17 14 91 43 58 50 27 29 48
63 66 04 68 89 53 67 30 73 16 69 87 40 31
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
", "NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)" ], "files": { "indexjs": { "key": "indexjs", "ext": "js", "name": "index", "contents": [ "function maximumPathSumI(triangle) {", " // Good luck!", " return true;", "}", "", "const testTriangle = [[3, 0, 0, 0],", " [7, 4, 0, 0],", " [2, 4, 6, 0],", " [8, 5, 9, 3]];", "", "maximumPathSumI(testTriangle);" ], "head": [ "const numTriangle = [[75, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [95, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [17, 47, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [18, 35, 87, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [20, 4, 82, 47, 65, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [19, 1, 23, 75, 3, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0], [88, 2, 77, 73, 7, 63, 67, 0, 0, 0, 0, 0, 0, 0, 0], [99, 65, 4, 28, 6, 16, 70, 92, 0, 0, 0, 0, 0, 0, 0], [41, 41, 26, 56, 83, 40, 80, 70, 33, 0, 0, 0, 0, 0, 0], [41, 48, 72, 33, 47, 32, 37, 16, 94, 29, 0, 0, 0, 0, 0], [53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14, 0, 0, 0, 0], [70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57, 0, 0, 0], [91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48, 0, 0], [63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31, 0], [4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]];" ], "tail": [] } } }, { "id": "5900f37f1000cf542c50fe92", "challengeType": 5, "title": "Problem 19: Counting Sundays", "tests": [ { "text": "countingSundays(1943, 1946) should return 6.", "testString": "assert.strictEqual(countingSundays(1943, 1946), 6, 'countingSundays(1943, 1946) should return 6.');" }, { "text": "countingSundays(1995, 2000) should return 9.", "testString": "assert.strictEqual(countingSundays(1995, 2000), 9, 'countingSundays(1995, 2000) should return 9.');" }, { "text": "countingSundays(1901, 2000) should return 171.", "testString": "assert.strictEqual(countingSundays(1901, 2000), 171, 'countingSundays(1901, 2000) should return 171.');" } ], "solutions": [ "function countingSundays(firstYear, lastYear) {\n let sundays = 0;\n\n for (let year = firstYear; year <= lastYear; year++) {\n for (let month = 1; month <= 12; month++) {\n const thisDate = new Date(year, month, 1);\n if (thisDate.getDay() === 0) {\n sundays++;\n }\n }\n }\n return sundays;\n}" ], "translations": {}, "description": [ "You are given the following information, but you may prefer to do some research for yourself.", "