-
Notifications
You must be signed in to change notification settings - Fork 18
/
Collatz Sequence.cs
85 lines (74 loc) · 1.9 KB
/
Collatz Sequence.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication210
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Generation of Collatz Sequence");
Console.WriteLine("\n");
Console.WriteLine("Enter the Number");
int N = Convert.ToInt32(Console.ReadLine());
int n;
n = N;
Console.WriteLine("\n");
Console.WriteLine("\n");
Console.Write("{0} ", N);
if (n % 2 == 0)
{
collatz(N);
}
else
{
collatz1(N);
}
}
private static void Exit(int value)
{
Console.Write("{0} ", value/2);
Console.ReadLine();
}
public static void collatz(int even)
{
int e = 0; e = even / 2;
if (e != 1)
{
Console.Write("{0} ", e);
if (e % 2 == 0)
{
collatz(e);
}
else
{
collatz1(e);
//return e;
}
}
else
{
Exit(even);
}
}
public static void collatz1(int odd)
{
int o=0,b=1,a,dummy=0;
a = (odd * 3) + b;
o = a;
Console.Write("{0} ", o);
if (o % 2 == 0)
{
collatz(o);
//return o;
}
else
{
collatz1(o);
//return o;
}
}
}
}