简体   繁体   English

欧拉计划#8 — Javascript

[英]Project Euler #8 — Javascript

 /*** * Problem 8 -- Largest Product in a Series * * @author RepeaterCreeper */ var grid = `73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450`; /** * Largest Adjacent Numbers * * @returns int */ function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\\n") { let gridData = grid.split("\\n"), largestProduct = 0; for (var row in gridData) { currentRow = gridData[row].split('').map(x => parseInt(x)); for (var i = 0; i < currentRow.length - consecutiveLength; i++) { combination = currentRow.slice(i, i + consecutiveLength); if (!combination.includes(0)) { product = combination.reduce(function(a, b) { return a * b }); if (largestProduct < product) largestProduct = product; } } } return largestProduct; } console.log(largestAdjacentNumbers(grid, 13)); 

Issue: 问题:

So the issue the fact that no matter what I do, my code is stuck at getting: 因此,无论我做什么,我的代码都会陷入以下事实:
5377010688 5377010688

The correct answer is: 正确的答案是:
23514624000 23514624000

As you can see that's a MASSIVE difference. 如您所见,这是巨大的差异。 If I simply do a 4-adjacent number in the grid, it works as it matches the solution given for the 4-adjacent number example. 如果我只是在网格中做一个4相邻的数字,它就可以匹配4相邻数字示例给出的解决方案。 Which is: 这是:

5832 5832


I've definitely looked into some other SO questions that relate to Problem 8 of Project Euler, but none of them seem to match my issue. 我当然也研究了与Euler项目的问题8有关的其他一些SO问题,但似乎没有一个与我的问题相符。 If I did somehow miss one, a link would be nice. 如果我确实以某种方式错过了一个链接,那将是一个很好的链接。 Though I do doubt that as I literally went through a good bit of SO questions before posting this. 尽管我确实怀疑,在我发帖之前,我确实经历了很多SO问题。


Problem States: 问题状态:

https://projecteuler.net/problem=8 -- Link to Problem https://projecteuler.net/problem=8-链接到问题

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 1000位数字中具有最大乘积的四个相邻数字为9×9×8×9 = 5832。

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 7163626956188267042825248360082 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 7163626956188267042825248360082 3257530420752963450 3257530420752963450

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. 在1000位数字中找到乘积最大的13位相邻数字。 What is the value of this product? 该产品的价值是什么?

The question says: 问题说:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 1000位数字中具有最大乘积的四个相邻数字为9×9×8×9 = 5832。

(some number over multiple lines, 1000 digits long) (多行中的某个数字,长度为1000位)

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. 在1000位数字中找到乘积最大的13位相邻数字。 What is the value of this product? 该产品的价值是什么?

It's not asking you to iterate over each row - it's asking you to interpret the whole thing as one string (or number) and compare adjacent digits. 并不是要您遍历每一 ,而是要您将整个内容解释为一个字符串(或数字)并比较相邻的数字。 Turn the grid into a single string initially, and your code works as expected: 首先将grid变成单个字符串,您的代码将按预期工作:

 var grid = `73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450` .split('\\n').join(''); /** * Largest Adjacent Numbers * * @returns int */ function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\\n") { let gridData = grid.split("\\n"), largestProduct = 0; for (var row in gridData) { currentRow = gridData[row].split('').map(x => parseInt(x)); for (var i = 0; i < currentRow.length - consecutiveLength; i++) { combination = currentRow.slice(i, i + consecutiveLength); if (!combination.includes(0)) { product = combination.reduce(function(a, b) { return a * b }); if (largestProduct < product) largestProduct = product; } } } return largestProduct; } console.log(largestAdjacentNumbers(grid, 13)); 

But it would be good to remove the part that iterates over each row now that there are no rows or newlines in the input, and to fix the off-by-one error, as noted in comment: 但是,由于输入中没有行或换行符,因此最好删除迭代在每一行上的部分,并修复逐个错误,如注释中所述:

 var grid = `73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450`; var gridData = grid.split('\\n').join('').split('').map(Number); /** * Largest Adjacent Numbers * * @returns int */ function largestAdjacentNumbers(grid, consecutiveLength = 4, separator = "\\n") { let largestProduct = 0; for (var i = 0; i <= gridData.length - consecutiveLength; i++) { combination = gridData.slice(i, i + consecutiveLength); if (!combination.includes(0)) { const product = combination.reduce(function(a, b) { return a * b }); if (largestProduct < product) largestProduct = product; } } return largestProduct; } console.log(largestAdjacentNumbers(grid, 13)); 

The problem apparently doesn't intend that the end of line has any meaning. 问题显然并不意味着行尾没有任何意义。 Here is code that produces the desired result. 这是产生所需结果的代码。

 let str = `73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450`; str = str.replace(/\\n/g, ""); let max = 0; for (let i = 0; i < str.length - 12; i++) { let part = str.substr(i, 13); let currentProduct = 1; for (let n of part) { currentProduct *= +n; } max = Math.max(max, currentProduct); } console.log(max); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM