-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
83 lines (68 loc) · 1.46 KB
/
Copy pathmain.cpp
File metadata and controls
83 lines (68 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
* File: main.cpp
* Author: henryhenderson
*
* Created on April 23, 2015, 6:31 PM
*/
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
const int TENMILL = 10000000;
const int TWO = 2;
int numOf89s = 0;
// Returns digit value at index
// Credit: http://stackoverflow.com/a/11354607
int getDigit(int from, int index)
{
return (from / (int)pow(10, floor(log10(from)) - index)) % 10;
}
// Returns int of length of int passed
// Eg: pass the number 483 will return '3'
int numLength(int n)
{
int digits = 0;
do {
++digits;
n /= 10;
} while (n);
return digits;
}
// Sums the squares of each digit in a number
// Eg: 42, 4^2 + 2^2 = 20
int sqSum(int num)
{
int sum = 0;
for (int i = 0; i < numLength(num); i++)
sum += pow(getDigit(num, i), TWO);
return sum;
}
int main(int argc, char** argv)
{
bool iterate = true;
int sum = 0;
for (int i = 1; i < TENMILL; i++)
{
sum = 0; // reset after each recurse ends
iterate = true;
while (iterate)
{
if (sum)
sum = sqSum(sum);
else
sum = sqSum(i);
switch (sum)
{
case 89:
numOf89s++;
case 1:
iterate = false;
continue;
}
}
}
cout << numOf89s << endl;
return 0;
}