-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem17.cs
181 lines (145 loc) · 4.79 KB
/
Problem17.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Text;
namespace Miller.ProjectEuler.Solutions
{
/// <summary>
/// Number letter counts
/// </summary>
/// <seealso cref="https://projecteuler.net/problem=17"/>
public class Problem17
{
/// <summary>
/// Converts each number up to and including the <paramref name="limit"/> to its English representation (e.g. 1 to "one") and then counts the number of letters.
/// </summary>
/// <param name="limit">The numbers to write out in sequence. The sequence includes the limit. Default is 1000.</param>
/// <returns>
/// The number of letters excluding hyphens and spaces.
/// </returns>
public int Solve(int limit = 1000)
{
var builder = new StringBuilder();
for (int i = 1; i <= limit; i++)
{
builder.Append(NumberAsWords(i));
}
var sequenceAsWords = builder.ToString();
var cleaned = sequenceAsWords.Replace("-", "").Replace(" ", ""); // problem states that hyphens and empty spaces should not be counted
return cleaned.Length;
}
/// <summary>
/// Converts <paramref name="n"/> to its English representation (e.g. 1 to "one").
/// <para>
/// Note that this method can handle only numbers up to and including 1000.
/// </para>
/// </summary>
/// <param name="n">Number to convert to its written form.</param>
/// <returns>
/// <paramref name="n"/> written out in English.
/// </returns>
/// <exception cref="Exception">Thrown when <paramref name="n"/> is greater than 1000.</exception>
public string NumberAsWords(int n)
{
if (n == 0)
{
return "";
}
if (n < 1 || n > 1000)
{
throw new ArgumentException($"{n} must be a positive integer less than one hundred.");
}
if (n < 20)
{
return GetNumberPartAsWord(n);
}
else if (20 <= n && n < 100)
{
var ones = GetNumberPartAsWord(n % 10);
var tens = GetNumberPartAsWord(n - (n % 10));
return $"{tens} {ones}";
}
else if (100 <= n && n < 1000)
{
var tens = NumberAsWords(n % 100);
var hundreds = GetNumberPartAsWord(n / 100);
if (string.IsNullOrEmpty(tens))
{
return $"{hundreds} hundred";
}
else
{
return $"{hundreds} hundred and {tens}";
}
}
else if (n == 1000)
{
return "one thousand";
}
else
{
throw new Exception("Cannot convert numbers greater than 1000 to its English representation");
}
}
private string GetNumberPartAsWord(int n)
{
switch (n)
{
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";
case 20:
return "twenty";
case 30:
return "thirty";
case 40:
return "forty";
case 50:
return "fifty";
case 60:
return "sixty";
case 70:
return "seventy";
case 80:
return "eighty";
case 90:
return "ninety";
default:
return "";
}
}
}
}