Hailstone Sequence so far

Good afternoon everyone,

I'm currently working towards to provide the following output for my code:

What number shall i start with? 7
The hailstone sequence starting at 7 is:
7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
The length of the sequence is 17.
The longest hailstone sequence starting with a number up to 7 has length 17
The longest hailstone sequence starting with a number up to 7 begins with 7

Below is what I've completed thus far:


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
86
87
88

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;


//Function Variable Declaration.
int lengthof_Sequence(int x), max_Sequence (int m), hailstone_Sequence (int n), count, length;


int main(int argc, char** argv)
{
    int user_Input, max_Input; 
    
    cout << "What number shall I start with? "; cin >> user_Input; cout << user_Input << endl;
    
    cout << "\nThe length of the sequence is " << hailstone_Sequence (user_Input) << ".";
    
    

    cout << "\nThe largest number in the sequence is: \n";

    cout << "The longest hailstone sequence starting " << user_Input << " with a number up to  has length " << count<< "." <<endl;

    cout << "The longest hailstone sequence starting with a number up to " << user_Input << " begins with " << user_Input;
    return 0;
}


    /* The hailstone sequence takes 'n' that is greater than 1 from the user. If
    // 'n' is even, computes n/2. If n is odd, computes 3n+1. Both are done till n = 1
    // is reached.
    */
    int hailstone_Sequence (int n)
    {
        int count = 1, length = 0;
        
        if (n > 1)
        {
            cout << "The hailstone of sequence starting at "  << n << " is: " << endl;
            cout << n << " ";
            
            while (n > 1)
            {
                
                if ((n % 2) == 0 )
                {
                   n = n / 2;
                }
                else
                {
                    n = 3 * n + 1;
                }
                cout << n << " "; 
                
                count++;
                
            }
            length = count;
        }
           return count;
    }

    /* Lengthof_Sequence 
    // returns *pending* 
    //
    */
    int lengthof_Sequence(int x)
    {
       
       
       
       
       
        
    }
        
    
    
    int max_Sequence (int m)  
    {
       
       
       
        
    }  



I'm having trouble coming up with a way to print the max and this line "The longest hailstone sequence starting 7 with a number up to has length____." for the output. Does anyone have any advice or suggestions?
Last edited on
Well the way you have the functions currently requires you to generate the hailstone sequence inside of each function. And if you do generate the sequence in each one, you could also keep up with the max or add a counter variable.
closed account (48T7M4Gy)
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>
#include <vector>

std::vector<int> make_hailstone_sequence (int n);
void display_sequence( const std::vector<int> &);

int main()
{
    const int no_sequences = 10;
    int number = 0;
    
    std::cout << "Please enter a number: ";
    std::cin >> number;
    
    std::vector<int> v[no_sequences];
    
    for(int i = 0; i < no_sequences; i++)
    {
        v[i] = make_hailstone_sequence(i + number);
        display_sequence(v[i]);
    }
    
    return 0;
}

std::vector<int> make_hailstone_sequence (int n)
{
    std::vector<int> hailstone_sequence;
    
    hailstone_sequence.push_back(n);
    
    while (n > 1)
    {
        if ( n % 2 == 0 )
            n = n / 2;
        else
            n = 3 * n + 1;
        
        hailstone_sequence.push_back(n);
    }
    
    return hailstone_sequence;
}

void display_sequence( const std::vector<int> &aSequence)
{
    std::cout << "Sequence: ";
    for(auto i:aSequence)
        std::cout << i << ' ';
    
    std::cout << " Size: " << aSequence.size() << '\n';
}
Please enter a number: 7
Sequence: 0  Size: 1
Sequence: 1  Size: 1
Sequence: 2 1  Size: 2
Sequence: 3 10 5 16 8 4 2 1  Size: 8
Sequence: 4 2 1  Size: 3
Sequence: 5 16 8 4 2 1  Size: 6
Sequence: 6 3 10 5 16 8 4 2 1  Size: 9
Sequence: 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1  Size: 17
Sequence: 8 4 2 1  Size: 4
Sequence: 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1  Size: 20
Last edited on
Testing the limits!

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
86
#include <iostream>
#include <cstdlib>
#include <limits>
#include <typeinfo>
using namespace std;

//typedef unsigned long long INT;
//typedef unsigned long      INT;
//typedef long               INT;
  typedef int                INT;

INT MAX = numeric_limits<INT>::max();
INT TRIGGER = MAX / 3.0 + 0.9;

INT hailstoneSequence( INT nstart, bool showSequence, bool showLength );

//===========

int main( int argc, char *argv[] )
{
   INT MaxLength = 0, nMaxLength = 0, length;
   INT nmax;
   INT i;
   bool showLength = false;
   bool showSequence = false;

   if ( argc > 1 )
   {                                   // maximum n taken from the command line
      nmax = atoi( argv[1] );
   }
   else                                // maximum n taken from the user at run time
   {
      cout << "Enter the maximum n: ";
      cin >> nmax;
      cout << endl;
   }

   for ( i = 1; i <= nmax; i++ )
   {
      length = hailstoneSequence( i, showSequence, showLength );
      if ( length > MaxLength )
      {
         MaxLength = length;
         nMaxLength = i;
      }
   }

   cout << endl;
   cout << "The longest sequence is for n = " << nMaxLength << endl;
   cout << "This sequence has length " << MaxLength << endl;
   cout << "The sequence is ";   hailstoneSequence( nMaxLength, true, false );
}

//==============

INT hailstoneSequence( INT nstart, bool showSequence, bool showLength )
{
    INT n = nstart;
    INT count = 1;

    if ( showSequence || showLength ) cout << n;

    while ( n != 1)
    {
       count++;
       if ( n % 2 == 0 ) 
       {
          n /= 2;
       }
       else if ( n >= TRIGGER )
       {
          cout << typeid(INT).name() << " bounds will be exceeded for starting point n = " << nstart << endl;
          exit( 1 );
       }
       else
       {
          n = 3 * n + 1;
       }

       if ( showSequence ) cout << " " << n;
    }

    if ( showLength ) cout << "      Length: " << count;
    if ( showSequence || showLength ) cout << endl;
    return count;
}


Using int type:

hailstone 10
The longest sequence is for n = 9
This sequence has length 20
The sequence is 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1



hailstone 1000

The longest sequence is for n = 871
This sequence has length 179
The sequence is 871 2614 1307 3922 1961 5884 2942 1471 4414 2207 6622 3311 9934 4967 14902 7451 22354 11177 33532 16766 8383 25150 12575 37726
18863 56590 28295 84886 42443 127330 63665 190996 95498 47749 143248 71624 35812 17906 8953 26860 13430 6715 20146 10073 30220 15110 7555
22666 11333 34000 17000 8500 4250 2125 6376 3188 1594 797 2392 1196 598 299 898 449 1348 674 337 1012 506 253 760 380 190 95 286 143 430 215
646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251
754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232
4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1



hailstone 1000000
i bounds will be exceeded for starting point n = 113383



Using unsigned long long:

hailstone 1000000
The longest sequence is for n = 837799
This sequence has length 525
The sequence is 837799 2513398 1256699 3770098 1885049 5655148 2827574 1413787 4241362 2120681 6362044 3181022 1590511 4771534 2385767

( --- a lot of lines manually removed --- )

1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911
2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1

Last edited on
closed account (48T7M4Gy)
Using vector of vectors:

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
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>

typedef unsigned long long int ULLI;
typedef std::vector<ULLI> HS_Sequence;
typedef std::vector<HS_Sequence> HS_Sequence_Set;

HS_Sequence make_hailstone_sequence (ULLI);
void display_sequence( const HS_Sequence &);
HS_Sequence longest_sequence( const HS_Sequence_Set);

int main()
{
    ULLI start_number = 0;
    ULLI end_number = 0;
    ULLI no_sequences = 0;
    ULLI temp = 0;
    HS_Sequence vTemp;
    HS_Sequence_Set v_set;
    
    // USER INPUT FOR VALUE RANGE
    std::cout << "Please enter a start followed by end limits (inclusive): ";
    std::cin >> start_number >> end_number;
    
    // REPAIR INPUT ERROR IF MADE
    if(end_number < start_number)
    {
        temp = start_number;
        start_number = end_number;
        end_number = temp;
    }
    no_sequences = end_number - start_number + 1;
    
    // GENERATE VECTOR OF SEQUENCE VECTORS
    for(ULLI i = 0; i < no_sequences; i++)
    {
        vTemp = make_hailstone_sequence(i + start_number);
        v_set.push_back(vTemp);
        display_sequence(vTemp);
        std::cout << '\n';
    }
    
    // LONGEST SEQUENCE IN GROUP
    vTemp = longest_sequence( v_set );
    std::cout << "Longest ";
    display_sequence(vTemp);
    
    std::cout << "Finished\n";
    return 0;
}

HS_Sequence make_hailstone_sequence (ULLI aNumber)
{
    HS_Sequence hailstone_sequence;
    
    hailstone_sequence.push_back(aNumber);
    while (aNumber > 1)
    {
        if ( aNumber % 2 == 0 )
            aNumber /= 2;
        else
            aNumber = 3 * aNumber + 1;
        
        hailstone_sequence.push_back(aNumber);
    }
    
    return hailstone_sequence;
}

void display_sequence( const HS_Sequence &aSequence)
{
    std::cout << "Sequence: ";
    for(auto i:aSequence)
        std::cout << i << ' ';
    
    std::cout << " Size: " << aSequence.size() << '\n';
}

HS_Sequence longest_sequence( const HS_Sequence_Set aSet)
{
    HS_Sequence hs_max = aSet[0];
    ULLI max_size = aSet.begin() -> size();
    
    for(auto i:aSet)
    {
        if ( i.size() > max_size )
        {
            max_size = i.size();
            hs_max = i;
        }
    }
    
    return hs_max;
}
Last edited on
Topic archived. No new replies allowed.