0

I'm trying to solve this problem on euler:

Large sum

Problem 13

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.

37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
...
53503534226472524250874054075591789781264330331690

I'm trying to concat all the lines into one big chunk of line, however I'm not getting the expected result, can anyone see what I did wrong here?

var fs  = require("fs");
var total = "\n";        
let m1 = fs.readFileSync('lines.txt').toString().split('\n').forEach(line => {
    total += line;
})
console.log(total)

output:

53503534226472524250874054075591789781264330331690

Process finished with exit code 0

Community
  • 1
  • 1
zuest
  • 167
  • 2
  • 15

1 Answers1

0

First to the last part of your question: the reason the output looks like that may be caused by the format of the input file. Possibly it has \r\n characters as line break sequence, so if you split the text by \n only, the \r will still print. As a result the last line could "overwrite" all previous lines in the output.

You could solve this by changing the split argument as follows:

.split(/\r?\n/)

To the problem you try to solve:

  • Trying to concatenate the lines is not going to solve the problem at hand. This way your are not doing anything with the numerical values that would bring you closer to the solution.
  • The question needs to know the last 10 digits of the sum. There is no attempt in your code to provide that. The reason for this 10 is that summing numbers with 50 digit-precision is beyond the capabilities of JavaScript, so you really need to do something with this.

Some hints:

  • Use slice(-10) to get the last 10 character of a line
  • Convert strings to numbers before doing any arithmatic
  • Use the modulo operator (%) to get the last 10 digits of any sum
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    OP specifically stated "What I'm trying to do is to concat all the lines into one big chunk of line". Even if his approach to the problem is incorrect, that's not what he's asking. He's asking why the lines are not concatting when running his code. – TheOdd Sep 15 '17 at 20:32
  • tl;dr don't solve the euler problem and give him the answer, give him the answer to the specific question he asked. – TheOdd Sep 15 '17 at 20:33
  • Second that. That's not what they asked. – georg Sep 15 '17 at 20:37
  • If he were asking for the answer to problem 13 directly, then his question would be broader basically just asking "How would I solve this problem". That is *not* what he did. He is specifically having trouble concatenating strings together in his code, and that is what he is asking help for. If he really does want the solution for problem 13 just posted here, then I suggest he broaden/edit his question. – TheOdd Sep 15 '17 at 20:43
  • Updated my answer to deal with the problem with the seemingly truncated output. – trincot Sep 15 '17 at 20:54
  • ty appreciate it – zuest Sep 15 '17 at 21:33
  • @trincot Yes, my question was how to concat them into one big string, and it did :) – zuest Sep 15 '17 at 22:11
  • You're welcome ;-) Note that if that is the only thing you wanted to do, then it is no different from removing the new lines. So instead of `split`, you could have used `replace(/\r?\n/g, '')`, and the return value would be the long string. – trincot Sep 16 '17 at 06:17