forked from Twiggecode/Integer-Sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCullenNumbers.cs
38 lines (34 loc) · 1.16 KB
/
CullenNumbers.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
using System;
namespace IntegerSequences.CullenNumbers
{
class CullenNumbers
{
// The first few Cullen numbers are: 1, 3, 9, 25, 65, 161, 385, 897, 2049, 4609, 10241, 22529, 49153, 106497
private static double Cullen(int n)
{
return n * Math.Pow(2, n) + 1;
}
public static void Main(string[] args)
{
Console.WriteLine("Please enter an integer n where n >= 0");
bool isValid = false;
int nTH = 0;
while (!isValid) // Check if user input an integer. If not, tries again until an integer is inputted
{
try
{
string input = Console.ReadLine();
nTH = int.Parse(input);
}
catch (FormatException)
{
Console.WriteLine("Please enter an integer n where n >= 0");
continue;
}
isValid = true;
}
String s = String.Format("The {0}th element from Cullen numbers is {1}", nTH + 1, Cullen(nTH));
Console.WriteLine(s);
}
}
}