-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathHailstone Sequence.cpp
More file actions
37 lines (30 loc) · 900 Bytes
/
Hailstone Sequence.cpp
File metadata and controls
37 lines (30 loc) · 900 Bytes
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
/*Start with any positive integer (an initial seed) and obtain a sequence of numbers by following these rules.
1. If the current number is even, divide it by two; else if it is odd, multiply it by three and add one.
2. Repeat.
Let’s try a few numbers to see what happens:
n=3; 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, …
n=4; 2, 1, 4, 2, 1, …
n=5; 16, 8, 4, 2, 1, 4, 2, 1, …
n=6; 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, …
n=7; 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, …*/
#include<bits/stdc++.h>
using namespace std;
//*HailSequence using recursion
void HailSequence(int n,int length){
if (length==0)
{
return;
}else if (n%2==0)
{
cout<<n<<" ";
HailSequence(n/2,length-1);
}else{
cout<<n<<" ";
HailSequence((3*n)+1,length-1);
}
return;
}
int main(){
HailSequence(13,10);
return 0;
}