-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler17.cs
More file actions
82 lines (72 loc) · 1.87 KB
/
euler17.cs
File metadata and controls
82 lines (72 loc) · 1.87 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
//This solves Euler #17
//If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace words
{
class Program
{
readonly static List<string> onesNumbers = new List<string> { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "" };
readonly static List<string> tensNumbers = new List<string> { "", "one", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
readonly static List<string> teensNumbers = new List<string> { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
private static int letterCount;
private static StreamWriter file;
static void Main()
{
file = new StreamWriter("C:\\whereEverYouWantIt\\num.txt");
for (int i = 1; i < 1000; i++)
{
var tens = (i % 100) / 10;
var ones = i % 10;
var hundreds = i / 100;
if (hundreds > 0)
{
p(onesNumbers[hundreds]);
p(" hundred ");
if (i % 100 != 0)
{
p("and");
}
}
if (tens == 1)
{
if (hundreds > 0)
{
p(" ");
}
p(teensNumbers[ones], true);
}
else
{
if (hundreds > 0)
{
p(" ");
}
p(tensNumbers[tens]);
if (tens > 0)
{
p(" ");
}
p(onesNumbers[ones], true);
}
}
p("one thousand", true);
file.WriteLine(letterCount.ToString()); //don't want to count this.
file.Close();
}
private static void p(string s, bool newLine = false)
{
letterCount += s.Count(char.IsLetter);
if (newLine)
{
file.WriteLine(s);
}
else
{
file.Write(s);
}
}
}
}