-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
59 lines (55 loc) · 1.63 KB
/
Program.cs
File metadata and controls
59 lines (55 loc) · 1.63 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _3xPlus1 {
internal class Program {
static string LogFile = "c:\\temp\\hailstone.txt";
static Int64 max = 9999999;
static void Main(string[] args)
{
int steps;
Int64 cnt = 1;
//for (Int64 cnt = 1; cnt <= max;cnt++) {
do {
Int64 x = cnt;
string nbrs = "";
steps = 0;
do {
x = getHailstoneNbr(x);
if (x == 1) {
nbrs = nbrs + x.ToString();
} else {
nbrs = nbrs + x.ToString() + ",";
}
steps++;
} while (x != 1);
WriteLog(cnt, steps, nbrs);
cnt++;
} while (cnt < max);
Console.ReadLine();
}
static Int64 getHailstoneNbr(Int64 n) {
Int64 hailstoneNbrs = n;
if (isOdd(hailstoneNbrs))
{
hailstoneNbrs = (hailstoneNbrs * 3) + 1;
}
else
{
hailstoneNbrs = (hailstoneNbrs / 2);
}
return hailstoneNbrs;
}
static bool isOdd(Int64 n) {
return n % 2 != 0;
}
private static void WriteLog(Int64 cnt, Int64 step, string nbrs) {
string line = $"{cnt},{step},[{nbrs}]{Environment.NewLine}";
Console.Write(line);
File.AppendAllText(LogFile, $"{line}");
}
}
}