1
var number = 342345820139586830203845861938475676
var output = []

var sum = 0;
while (number) {
    output.push(number % 10);
    number = Math.floor(number/10);
}

output = output.reverse();


function addTerms () {
    for (i = 0; i < output.length; i=i+2) {
    var term = Math.pow(output[i], output[i+1]);
    sum += term;

}
return sum;
}

document.write(output);
document.write("<br>");
document.write(addTerms());

I am trying to take that large number and split it into its digits. Then, find the sum of the the first digit raised to the power of the 2nd, 3rd digit raiseed to the 4th, 5th raised to the 6th and so on. for some reason, my array is returning weird digits, causing my sum to be off. the correct answer is 2517052. Thanks

Nic Meiring
  • 882
  • 5
  • 16
  • 33

7 Answers7

1

You're running into precision issues within JavaScript. Just evaluate the current value of number before you start doing anything, and the results may surprise you:

>>> var number = 342345820139586830203845861938475676; number;
3.423458201395868e+35

See also: What is JavaScript's highest integer value that a Number can go to without losing precision?

To resolve your issue, I'd store your input number as an array (or maybe even a string), then pull the digits off of that.

This will solve your calculation with the expected result of 2517052:

var number = "342345820139586830203845861938475676";

var sum = 0;
for(var i=0; i<number.length; i=i+2){
  sum += Math.pow(number.charAt(i), number.charAt(i+1));
}

sum;
Community
  • 1
  • 1
ziesemer
  • 27,712
  • 8
  • 86
  • 94
1

JavaScript stores numbers in floating point format (commonly double). double can store precisely only 15 digits.

You can use string to store this large number.

artyom.stv
  • 2,097
  • 1
  • 24
  • 42
1

Cast the number as a string and then iterate through it doing your math.

var number = "342345820139586830203845861938475676";//number definition
var X = 0;//some iterator
var numberAtX = 0 + number.charAt(X);//number access
Travis J
  • 81,153
  • 41
  • 202
  • 273
  • 1
    That doesn't overcome the problem that Javascript can't represent a 36-digit number. In your code, number will be equal to the *string* "3.423458201395868e+35" (depending on the platform) which isn't much use. – Borodin Feb 04 '12 at 10:22
  • Indeed it does render a number first, the simple change would be to surround the whole number with quotes. Edited to show. – Travis J Feb 04 '12 at 11:07
1

As mentioned, this is a problem with numeric precision. It applies to all programming languages that use native numeric formats. Your problem works fine if you use a string instead

var number = '342345820139586830203845861938475676'
var digits = number.split('')
var total = 0
while (digits.length > 1) {
  var [n, power] = digits.splice(0, 2)
  total += Math.pow(n, power)
}

(the result is 2517052, byt the way!)

Borodin
  • 126,100
  • 9
  • 70
  • 144
0

The greatest integer supported by Javascript is 9007199254740992. So that only your output is weird.

For Reference go through the link http://ecma262-5.com/ELS5_HTML.htm#Section_8.5

AmGates
  • 2,127
  • 16
  • 29
0

[edit] adjusted the answer based on Borodins comment.

Mmm, I think the result should be 2517052. I'd say this does the same:

var numbers = '342345820139586830203845861938475676'.split('')
   ,num = numbers.splice(0,2)
   ,result = Math.pow(num[0],num[1]);
while ( (num = numbers.splice(0,2)) && num.length ){
  result += Math.pow(num[0],num[1]);
}
console.log(result); //=> 2517052
Community
  • 1
  • 1
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • 1
    You are shifting only one digit at a time from the string, producing 3^4 + 4^2 + 2^3 + 3^4 etc. The OP wanted 3^4 + 2^3 + 4^5 + 8^2 etc. – Borodin Feb 04 '12 at 10:30
  • You're right, missed it. Adjusted the answer. It's basically the same solution as your answer with a slight adjustment in the `while` loop – KooiInc Feb 04 '12 at 10:45
0

The array methods map and reduce are supported in modern browsers, and could be worth defining in older browsers. This is a good opportunity, if you haven't used them before.

If you are going to make an array of a string anyway, match pairs of digits instead of splitting to single digits. This example takes numbers or strings.

function sumPower(s){
    return String(s).match(/\d{2}/g).map(function(itm){
        return Math.pow(itm.charAt(0), itm.charAt(1));
    }).reduce(function(a, b){
        return a+b;
    });
}

sumPower('342345820139586830203845861938475676');
alert(sumPower(s))
/*  
returned value:(Number)
2517052
*/
kennebec
  • 102,654
  • 32
  • 106
  • 127