diff --git a/challenges/08-coding-interview-prep/project-euler.json b/challenges/08-coding-interview-prep/project-euler.json index e857ec2de..8674314f2 100644 --- a/challenges/08-coding-interview-prep/project-euler.json +++ b/challenges/08-coding-interview-prep/project-euler.json @@ -97,7 +97,7 @@ "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." + "By considering the terms in the Fibonacci sequence whose values do not exceed nth term, find the sum of the even-valued terms." ], "files": { "indexjs": { @@ -105,7 +105,7 @@ "ext": "js", "name": "index", "contents": [ - "function fiboEvenSum(number) {", + "function fiboEvenSum(n) {", " // You can do it!", " return true;", "}", @@ -160,7 +160,7 @@ "translations": {}, "description": [ "The prime factors of 13195 are 5, 7, 13 and 29.", - "What is the largest prime factor of the number 600851475143 ?" + "What is the largest prime factor of the given number?" ], "files": { "indexjs": { @@ -204,7 +204,7 @@ "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." + "Find the largest palindrome made from the product of two n-digit numbers." ], "files": { "indexjs": { @@ -212,7 +212,7 @@ "ext": "js", "name": "index", "contents": [ - "function largestPalindromeProduct(digit) {", + "function largestPalindromeProduct(n) {", " // Good luck!", " return true;", "}", @@ -234,11 +234,21 @@ "testString": "assert.strictEqual(smallestMult(5), 60, 'smallestMult(5) should return 60.');" }, + { + "text": "smallestMult(7) should return 420.", + "testString": + "assert.strictEqual(smallestMult(7), 420, 'smallestMult(7) should return 420.');" + }, { "text": "smallestMult(10) should return 2520.", "testString": "assert.strictEqual(smallestMult(10), 2520, 'smallestMult(10) should return 2520.');" }, + { + "text": "smallestMult(13) should return 360360.", + "testString": + "assert.strictEqual(smallestMult(13), 360360, 'smallestMult(13) should return 360360.');" + }, { "text": "smallestMult(20) should return 232792560.", "testString": @@ -251,7 +261,7 @@ "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?" + "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n?" ], "files": { "indexjs": { @@ -303,7 +313,7 @@ "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." + "Find the difference between the sum of the squares of the first n natural numbers and the square of the sum." ], "files": { "indexjs": { @@ -311,7 +321,7 @@ "ext": "js", "name": "index", "contents": [ - "function sumSquareDifference(number) {", + "function sumSquareDifference(n) {", " // Good luck!", " return true;", "}", @@ -360,7 +370,7 @@ "translations": {}, "description": [ "By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.", - "What is the 10 001st prime number?" + "What is the nth prime number?" ], "files": { "indexjs": { @@ -368,7 +378,7 @@ "ext": "js", "name": "index", "contents": [ - "function nthPrime(number) {", + "function nthPrime(n) {", " // Good luck!", " return true;", "}", @@ -424,7 +434,7 @@ "
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?" + "Find the n adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?" ], "files": { "indexjs": { @@ -432,7 +442,7 @@ "ext": "js", "name": "index", "contents": [ - "function largestProductinaSeries(number) {", + "function largestProductinaSeries(n) {", " // 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,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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;", @@ -477,7 +487,7 @@ "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." + "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc such that a + b + c = n." ], "files": { "indexjs": { @@ -599,7 +609,7 @@ "
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?" + "What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in a given arr grid?" ], "files": { "indexjs": { @@ -668,9 +678,21 @@ }, { "text": - "divisibleTriangleNumber() should return 76576500.", + "divisibleTriangleNumber(167) should return 1385280.", "testString": - "assert.strictEqual(divisibleTriangleNumber(500), 76576500, 'divisibleTriangleNumber() should return 76576500.');" + "assert.strictEqual(divisibleTriangleNumber(167), 1385280, 'divisibleTriangleNumber(167) should return 1385280.');" + }, + { + "text": + "divisibleTriangleNumber(374) should return 17907120.", + "testString": + "assert.strictEqual(divisibleTriangleNumber(374), 17907120, 'divisibleTriangleNumber(374) should return 17907120.');" + }, + { + "text": + "divisibleTriangleNumber(500) should return 76576500.", + "testString": + "assert.strictEqual(divisibleTriangleNumber(500), 76576500, 'divisibleTriangleNumber(500) should return 76576500.');" } ], "solutions": [ @@ -689,7 +711,7 @@ "
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?" + "What is the value of the first triangle number to have over n divisors?" ], "files": { "indexjs": { @@ -983,6 +1005,18 @@ "testString": "assert.strictEqual(longestCollatzSequence(5847), 3711, 'longestCollatzSequence(5847) should return 3711.');" }, + { + "text": + "longestCollatzSequence(46500) should return 35655.", + "testString": + "assert.strictEqual(longestCollatzSequence(46500), 35655, 'longestCollatzSequence(46500) should return 35655.');" + }, + { + "text": + "longestCollatzSequence(54512) should return 52527.", + "testString": + "assert.strictEqual(longestCollatzSequence(54512), 52527, 'longestCollatzSequence(54512) should return 52527.');" + }, { "text": "longestCollatzSequence(1000000) should return 837799.", @@ -1001,7 +1035,7 @@ "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?", + "Which starting number, under the given limit, produces the longest chain?", "NOTE: Once the chain starts the terms are allowed to go above one million." ], "files": { @@ -1052,7 +1086,7 @@ "", "\"a", "", - "How many such routes are there through a 20×20 grid?" + "How many such routes are there through a given gridSize?" ], "files": { "indexjs": { @@ -1099,7 +1133,7 @@ "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?" + "What is the sum of the digits of the number 2exponent?" ], "files": { "indexjs": { @@ -1146,7 +1180,7 @@ "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? ", + "If all the numbers from 1 to given limit 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": { @@ -1352,7 +1386,9 @@ "assert.strictEqual(sumAmicableNum(10000), 31626, 'sumAmicableNum(10000) should return 31626.');" } ], - "solutions": [], + "solutions": [ + "const sumAmicableNum = (n) => {\n const fsum = (n) => {\n let sum = 1;\n for (let i = 2; i <= Math.floor(Math.sqrt(n)); i++)\n if (Math.floor(n % i) === 0)\n sum += i + Math.floor(n / i);\n return sum;\n };\n let d = [];\n let amicableSum = 0;\n for (let i=2; in) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).", @@ -1547,27 +1583,29 @@ "title": "Problem 25: 1000-digit Fibonacci number", "tests": [ { - "text": "digitFibonacci(5) should return 20.", + "text": "digitFibonacci(5) should return 21.", "testString": - "assert(digitFibonacci(5) == 20, 'digitFibonacci(5) should return 20.');" + "assert.strictEqual(digitFibonacci(5), 21, 'digitFibonacci(5) should return 21.');" }, { - "text": "digitFibonacci(10) should return 44.", + "text": "digitFibonacci(10) should return 45.", "testString": - "assert(digitFibonacci(10) == 44, 'digitFibonacci(10) should return 44.');" + "assert.strictEqual(digitFibonacci(10), 45, 'digitFibonacci(10) should return 45.');" }, { - "text": "digitFibonacci(15) should return 68.", + "text": "digitFibonacci(15) should return 69.", "testString": - "assert(digitFibonacci(15) == 68, 'digitFibonacci(15) should return 68.');" + "assert.strictEqual(digitFibonacci(15), 69, 'digitFibonacci(15) should return 69.');" }, { - "text": "digitFibonacci(20) should return 92.", + "text": "digitFibonacci(20) should return 93.", "testString": - "assert(digitFibonacci(20) == 92, 'digitFibonacci(20) should return 92.');" + "assert.strictEqual(digitFibonacci(20), 93, 'digitFibonacci(20) should return 93.');" } ], - "solutions": [], + "solutions": [ + "const digitFibonacci = (n) => {\n const digits = (num) => {\n return num.toString().length;\n };\n let f1 = 1;\n let f2 = 1;\n let index = 3;\n while (true) {\n let fn = f1 + f2;\n if (digits(fn) === n) return index;\n [f1, f2] = [f2, fn];\n index++;\n }\n};" + ], "translations": {}, "description": [ "The Fibonacci sequence is defined by the recurrence relation:", @@ -1786,7 +1824,9 @@ "assert.strictEqual(distinctPowers(30), 755, 'distinctPowers(30) should return 755.');" } ], - "solutions": [], + "solutions": [ + "const distinctPowers = (n) => {\n let list = [];\n for (let a=2; a<=n; a++) {\n for (let b=2; b<=n; b++) {\n let term = Math.pow(a, b);\n if (list.indexOf(term)===-1) list.push(term);\n }\n }\n return list.length;\n};" + ], "translations": {}, "description": [ "Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:", @@ -1846,10 +1886,10 @@ "translations": {}, "description": [ "Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:", - "1634 = 14 + 64 + 34 + 44", - "8208 = 84 + 24 + 04 + 84", - "9474 = 94 + 44 + 74 + 44", - "As 1 = 14 is not a sum it is not included.", + "1634 = 14 + 64 + 34 + 44", + "8208 = 84 + 24 + 04 + 84", + "9474 = 94 + 44 + 74 + 44", + "As 1 = 14 is not a sum it is not included.", "The sum of these numbers is 1634 + 8208 + 9474 = 19316.", "Find the sum of all the numbers that can be written as the sum of n powers of their digits." ], @@ -2085,7 +2125,9 @@ "assert(circularPrimes(1000000) == 55, 'circularPrimes(1000000) should return 55.');" } ], - "solutions": [], + "solutions": [ + "const circularPrimes = (n) => {\n const primeCheck = (num) => {\n if (num === 1) {\n return false;\n }\n for (let i = 2; i <= Math.floor(Math.sqrt(num)); i++) {\n if (num % i === 0) {\n return false;\n }\n }\n return true;\n };\n let count = 1;\n for (let i = 1; i < n; i += 2) {\n if (primeCheck(i)) {\n let flag = true;\n let circularNum = i.toString();\n for (let j = 1; j < i.toString().length; j++) {\n circularNum = circularNum.substring(1) + circularNum.substring(0, 1);\n if (primeCheck(Number(circularNum)) === false) {\n flag = false;\n break;\n }\n }\n if (flag) {\n count++;\n }\n }\n }\n return count;\n};" + ], "translations": {}, "description": [ "The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.", @@ -3017,12 +3059,34 @@ "title": "Problem 55: Lychrel numbers", "tests": [ { - "text": "euler55() should return 249.", + "text": "countLychrelNumbers(1000) should return 13.", + "testString": + "assert.strictEqual(countLychrelNumbers(1000), 13, 'countLychrelNumbers(1000) should return 13.');" + }, + { + "text": "countLychrelNumbers(5000) should return 76.", "testString": - "assert.strictEqual(euler55(), 249, 'euler55() should return 249.');" + "assert.strictEqual(countLychrelNumbers(5000), 76, 'countLychrelNumbers(5000) should return 76.');" + }, + { + "text": "countLychrelNumbers(10000) should return 249.", + "testString": + "assert.strictEqual(countLychrelNumbers(10000), 249, 'countLychrelNumbers(10000) should return 249.');" + }, + { + "text": "Your function should count all Lychrel numbers.", + "testString": + "assert.strictEqual(countLychrelNumbers(3243), 39, 'Your function should count all Lychrel numbers.');" + }, + { + "text": "Your function should pass all test cases.", + "testString": + "assert.strictEqual(countLychrelNumbers(7654), 140, 'Your function should pass all test cases.');" } ], - "solutions": [], + "solutions": [ + "const countLychrelNumbers = (size) => {\n const numReverse = (num) => {\n return Number(num.toString().split('').reverse().join(''));\n };\n const isPalin = (num) => {\n if (numReverse(num) === num) {\n return true;\n }\n return false;\n };\n let total = 0;\n for (let i = 1; i < size; i++) {\n let loopCount = 1;\n let sum = i;\n while (loopCount < 50) {\n sum = sum + numReverse(sum);\n if (isPalin(sum)) {\n break;\n } else {\n loopCount++;\n }\n }\n if (loopCount === 50) {\n total++;\n }\n }\n return total;\n}" + ], "translations": {}, "description": [ "If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.", @@ -3033,7 +3097,7 @@ "That is, 349 took three iterations to arrive at a palindrome.", "Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).", "Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.", - "How many Lychrel numbers are there below ten-thousand?", + "How many Lychrel numbers are there below num?", "NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers." ], "files": { @@ -3042,12 +3106,12 @@ "ext": "js", "name": "index", "contents": [ - "function euler55() {", + "function countLychrelNumbers(num) {", " // Good luck!", " return true;", "}", "", - "euler55();" + "countLychrelNumbers(10000);" ], "head": [], "tail": []