About hailstone sequence

Sep 7, 2020 at 4:16am
Write your question here.

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
  #include <iostream>

using namespace std;

void hailstone( int = 1 );

int main()
{
    int A,n;

    cout <<"Number:" ;
    cin >> A;

    if (A<1){
        cout<<"Invalid number, please try again."<<endl;

    }
    if (A==1){
        cout<<"1"<<endl;
    }
    else{

        hailstone(A);
    }

    return 0;
}


void hailstone(int n)
 {

    int m;

     cout<<"Limit of terms: ";
     cin >> m;//To decide when to print this.
     do{
     for (int i = 0; i < m; i++)
     {

         if (n%2 == 0)
			n=n/2;
		else
			n=(3*n)+1;

		cout << n << endl;
     }

	}while(n>1);


     }


1. 'Limit of terms:' works, and when it reaches 1 it should stop regardless of this but it continues.
2.When printing numbers, input number must also be printed first.

example out put
Number: 15
Limit of terms: 50
15
46
23
70
35
106
53
160
80
40
20
10
5
16
8
4
2
1

please help me.
Last edited on Sep 7, 2020 at 4:25am
Sep 7, 2020 at 4:36am
To give you an idea:

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
#include <iostream>

void hailstoneSequence(int);

int main()
{
	int n{};
	std::cout << "Starting Number: ";
	std::cin >> n;

	hailstoneSequence(n);
}

void hailstoneSequence(int n)
{
	while (n != 1) // As long as the n value is not 1, keep looping. Stop when n is 1.
	{
		if (0 == n % 2)
		{
			n = n / 2;
		}
		else
		{
			n = (3 * n) + 1;
		}

		std::cout << n << std::endl;
	}
}


Source: https://plus.maths.org/content/mathematical-mysteries-hailstone-sequences
Sep 7, 2020 at 5:05am
Thank you. But the'limit of term' where I decide how long the loop will run is not resolved.
Sep 7, 2020 at 6:01am
The inner loop will go until m. Hence the outer do...while loop is useless.

Instead (remove the outer do...whild loop) add a break statement like so:
1
2
3
4
5
6
7
8
9
10
     for (int i = 0; i < m; i++)
     {

...
		std::cout << n << std::endl;

		if(n <= 1)
		  break;

     }
Sep 7, 2020 at 6:25am
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
#include <iostream>

using namespace std;

void hailstone( int = 1 );

int main()
{
    int A, n;
    
    cout << "Number: " ;
    cin >> A;
    
    if (A < 1)
    {
        cout<<"Invalid number, please try again."<<endl;
    }
    
    if (A == 1)
    {
        cout<<"1"<<endl;
    }
    else{
        
        hailstone(A);
    }
    
    return 0;
}


void hailstone(int n)
{
    int m{0};
    
    cout<<"Limit of terms: ";
    cin >> m;//To decide when to print this.
    
    int i{0};
    
    do{
        if (n % 2 == 0)
            n = n/2;
        else
            n = (3 * n) + 1;
        
        cout << n << endl;
        i++;
    } while(n > 1 and i < m);
}


Number: 1728
Limit of terms: 5
864
432
216
108
54
Program ended with exit code: 0
Sep 7, 2020 at 6:36am
Thank you so much!:)
Sep 7, 2020 at 6:51am
https://en.wikipedia.org/wiki/Collatz_conjecture
You might/will need to extend 'int' to 'long' and beyond. Python is good for these sort of calculations too :)
Topic archived. No new replies allowed.