-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler17.cpp
110 lines (97 loc) · 1.59 KB
/
Euler17.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <math.h>
#include <unistd.h>
#include <fstream>
using namespace std;
//return number of characters, (one ... nine)
int ones(int n){
if(n == 3 || n == 7 || n == 8){
return 5;
}
else if(n == 1 || n == 2 || n == 6){
return 3;
}
else if(n == 4 || n == 9 || n == 5){
return 4;
}
else if (n == 0){
return 0;
}
}
//return number of characters, (twenty ... ninety)
int tens(int n){
if(n == 4 || n == 5 || n == 6){
return 5;
}
else if(n == 2 || n == 3){
return 6;
}
else if(n == 8 || n == 9){
return 6;
}
else if(n == 7){
return 7;
}
}
//return number of characters (ten ... nineteen)
int teens(int n){
if(n == 11 || n == 12){
return 6;
}
else if(n == 13 || n == 14 || n == 18 || n == 19){
return 8;
}
else if(n == 15 || n == 16){
return 7;
}
else if(n == 17){
return 9;
}
else if(n == 10){
return 3;
}
}
int main(){
int sum = 0;
for(unsigned int i = 1; i <= 1000; i++){
if(i < 20){
if(i < 10){
sum += ones(i);
}
if(i >= 10 && i <= 19){
sum += teens(i);
}
}
if(i >= 20 && i <= 99){
sum += tens(i/10);
sum += ones(i%10);
}
if(i >= 100 && i <= 999){
sum += 7; //account for the "hundred"
if(i % 100 != 0){
sum += 3; //account for the "and"
}
int h = i/100;
int t = (i % 100)/10;
int o = i % 10;
sum += ones(h);
if(t == 0){
sum += ones(o);
}
if(t == 1){
sum += teens(t*10 + o);
}
if(t >= 2){
sum += tens(t);
sum += ones(o);
}
}
if(i == 1000){
sum += 3;
sum += 8;
}
}
cout<<"Sum: "<<sum<<endl;
}