One of my favorite things about Hack Reactor is that every day starts with a technical problem. Within the school, we refer to these problems as ‘toy problems.’

Since graduation, I’ve been devoting a few minutes every day to working through a comprehensive set of toy problems available at project euler. Over the weekend I encountered problem 16 and found it to be particularly interesting.

As it turns out, math, in JavaScript, is weird. Take, for instance, the following code:

```
var sum = 0;
for(var i = 0; i < 100; i++){
sum += 1/100
}
```

So, after running that code, what might `sum === 1`

evaluate to?

I kid you not, it is false. `sum`

is equal to `1.0000000000000007`

.

In trying to solve euler 16 (sum all digits in 2^1000) I similarly assumed that JavaScript would be free of quirks. In my initial attempt, I thought I could simply raise two to the thousandth power, convert it to a string, and then loop through each character and it to a sum. Easy, right?

Unfortunately, that didn’t work. The following code: `Math.pow(2,100)+''`

yields `'1.0715086071862673e+301'`

. This is not anywhere near an exact representation of 2^1000. The precise value is:

```
10715086071862673209484250490600018105614048117055336074437
50388370351051124936122493198378815695858127594672917553146
82518714528569231404359845775746985748039345677748242309854
21074605062371141877954182153046474983581941267398767559165
54394607706291457119647768654216766042983165262438683720566
8069376.
```

To get around the precision hurdle I found the largest power of two that Chrome represents without using scientific notation. Aha! If I just punch in `Math.pow(2,69)`

that yields `590295810358705700000`

. Solution found; now that I have the precise representation I can just multiply each place by 2 until I raise the exponent from 69 to 1000. Wrong. 2^69 isn’t `590295810358705700000`

; it’s really `590295810358705651712`

.

The real solution, as it turns out, is a rather mundane one; you just set up an array of integers and perform sequential arithmetic operations on those integers (multiplying by two, specifically) 999 times.

The sum of all digits that comprise 2^1000 is 1366. The full solution is available on github. Toy problem accomplished. JavaScript arithmetic lessons learned.