A
A
Artem Rassadin2020-11-09 12:31:16
JavaScript
Artem Rassadin, 2020-11-09 12:31:16

How to solve the 13th Euler problem?

My decision:

let sum = 0; // сумма чисел
let a = 1; // элемент по счету с конца

function count() {
  let arr = []; // массив для чисел, которые суммируются

  for (let i = 0; i < array.length; i++) { // проходимся по массивам
    let str = array[i].toString().split(''); // отделяем числа друг от друга
    let number = str.slice([str.length - a], [str.length]); // берем числа с конца начиная от 1 
    arr.push(parseInt(number.join(''))) // соединяем числа которые брали и переносим их в числовой тип
  }

  let protect = arr.reduce((total, sum) => total + sum, 0); // сумма всех этим чисел

  sum += Number(protect); // прибавляем к sum

  if (String(sum).length < 10) { // проверяем длину sum
    a++;
    sum = 0;
    count();
  } else {
    console.log(sum) // выводим сумму
  }
}
count()


The code works, but the answer is incorrect (at least the answer is not accepted as correct on the site).
Tell me what's wrong. You can create an array with numbers using the link: https://projecteuler.net/problem=13
It's too big to fit here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twolegs, 2020-11-09
@twolegs

The number type in js is not an integer, so there may be losses when summing such large numbers.
Try to use BigInt, maybe it will work with it. But in general, this problem involves an analytical solution, not a head-on solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question