-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.go
113 lines (101 loc) · 1.93 KB
/
17.go
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
111
112
113
package main
import (
"fmt"
"strings"
//"github.com/trevorgk/project-euler/eulerlib"
)
// If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
func euler_17() {
sum := 0
for i := 1; i <= 1000; i++ {
numericText := strings.TrimSpace(chunk(i))
sum += len(strings.Replace(numericText, " ", "", -1))
}
fmt.Println("Answer is:", sum)
}
func chunk(num int) string {
million, thousand, hundred := 1000000, 1000, 100
if (num >= million) {
mod := num%million
if (mod == 0) {
return chunk(num/million) + " million"
}
return chunk(num/million) + " million " + chunk(num%million)
}
if (num >= thousand) {
mod := num%thousand
if (mod == 0) {
return chunk(num/thousand) + " thousand"
}
return chunk(num/thousand) + " thousand " + chunk(num%thousand)
}
if (num > hundred) {
mod := num%hundred
if (mod == 0) {
return chunk(num/hundred) + " hundred"
}
return chunk(num/hundred) + " hundred and " + chunk(num%hundred)
}
if (num >= 20){
rem := num%10
switch (num / 10){
case 2:
return "twenty " + chunk(rem)
case 3:
return "thirty " + chunk(rem)
case 4:
return "forty " + chunk(rem)
case 5:
return "fifty " + chunk(rem)
case 6:
return "sixty " + chunk(rem)
case 7:
return "seventy " + chunk(rem)
case 8:
return "eighty " + chunk(rem)
case 9:
return "ninety " + chunk(rem)
}
}
switch(num){
case 1:
return "one"
case 2:
return "two"
case 3:
return "three"
case 4:
return "four"
case 5:
return "five"
case 6:
return "six"
case 7:
return "seven"
case 8:
return "eight"
case 9:
return "nine"
case 10:
return "ten"
case 11:
return "eleven"
case 12:
return "twelve"
case 13:
return "thirteen"
case 14:
return "fourteen"
case 15:
return "fifteen"
case 16:
return "sixteen"
case 17:
return "seventeen"
case 18:
return "eighteen"
case 19:
return "nineteen"
}
return ""
}