i'm trying to write a program that takes two numbers from the user prints out the sequence between the 2 numbers and then outputs wthe longest sequence

for example if the user inputed 8 and 10

it should out put

8 4 2 1
9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 5 16 8 4 2 1

i got the code as follows that does it for 1 number

#include <iostream>
using namespace std;


int f(int n);


int main()
{
int first;
cout << "Enter First Integer: ";
cin >> first;


while(first != 1) //test ithat first is not qual to 1
{
cout <<  first << " ";



first = f(first);//call function


}
cout << first << endl;


return 0;
}


int f(int n)
{


if(n %2 == 0)//test to see if even
{
return n /=2;


}
else
{
return n = 3*n+1;//is odd


}


}

i need it to do it for two numbers and out but the sequence between those two numbers and then compare them and output that number 9 is the longest sequence it contained 20 numbers any help with this would greatly be appreeciated. forgive me i'm new to the forum and i'm not sure how to use
the code tags.

Dani AI

Generated

Original poster asked for a program that prints every hailstone (3n+1) sequence for the integers in a user-supplied range and then reports which starting value produced the longest sequence. The OP later noted the problem was solved; asked for the code and reminded readers not to resurrect very old threads. Below is a concise, practical approach plus a robust C++ example that fills the gap left by the thread (validation, overflow checks, and a memoized length cache for speed).

Key points and tips:

  • Require positive integers and swap bounds if the first is larger than the second.
  • Use a 64-bit unsigned type for sequence values to delay overflow. Detect potential overflow before computing 3*n+1.
  • Memoize computed lengths in an unordered_map<unsigned long long,int> with base case length(1) = 1. This avoids recomputing long tails and makes ranges like 1..1e6 feasible.
  • Printing every full sequence for very large ranges is slow; build and print the full sequence only when needed (the example prints each sequence but comments note the performance tradeoff).
  • For truly huge inputs that exceed 64-bit, switch to a big-integer library (for example, Boost.Multiprecision).

Example (compact, production-minded):

#include <iostream>
#include <vector>
#include <unordered_map>
#include <limits>
#include <stdexcept>
#include <algorithm>

using ull = unsigned long long;

int collatz_length(ull n, std::unordered_map<ull,int>& cache) {
    if (n == 0) return 0;
    if (auto it = cache.find(n); it != cache.end()) return it->second;
    ull m = n;
    std::vector<ull> path;
    while (cache.find(m) == cache.end()) {
        path.push_back(m);
        if ((m & 1ull) == 0) m /= 2;
        else {
            if (m > (std::numeric_limits<ull>::max() - 1) / 3)
                throw std::overflow_error("value overflow during collatz");
            m = 3*m + 1;
        }
    }
    int len = cache[m];
    for (int i = static_cast<int>(path.size()) - 1; i >= 0; --i) {
        len += 1;
        cache[path[i]] = len;
    }
    return cache[n];
}

std::vector<ull> make_sequence(ull n) {
    std::vector<ull> s;
    while (true) {
        s.push_back(n);
        if (n == 1) break;
        if ((n & 1ull) == 0) n /= 2;
        else {
            if (n > (std::numeric_limits<ull>::max() - 1) / 3)
                throw std::overflow_error("value overflow during collatz");
            n = 3*n + 1;
        }
    }
    return s;
}

// main: read two positive integers, iterate range, print sequences and report longest

Common pitfalls: negative or zero inputs, 32-bit overflow (use 64-bit), very large ranges that cause long runtimes or big memory use when memoizing, and resurrecting stale threads without adding value (the community note from ). The example above is safe for typical classroom inputs and highlights where to add big-integer support or limit printing for large ranges.

Recommended Answers

All 5 Replies

do you know how to use interpunction?
your ramblings are completely incomprehansible.

Here is the code so far now where i'm stuck is how to put the first number into a temp variable call my function and then store the count into a variable and compare the count with the next number called after calling the function.

For example the numbers entered are 8 and 10
after the first call to my function the result is:

8 4 2 1 count = 4

next call

9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 count2 = 20

last call
10 5 16 8 4 2 1 count3 = 7

once i compared the counter i found the longest sewuence out put the longest sequence, i'm stuck as to where to count and then compare the reults here is what i have so far.

do you know how to use interpunction?
your ramblings are completely incomprehansible.

thanks everyone for your help, i no longer need help with this program, got it all figured out and it's working perfectly.

Could you post your working code for the benifit of the community? Thanks in advance.

For the benifit of the community could you refrain from digging up threads almost 2 years old ? Thanks in advance.
If you have the same problem please create a new thread with your effort upto now. :) If you want you can post a link to this thread from your post, so that we know what you are talking about.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.