I am very very new to C+= programming and this is my first assignment.Can some one please help me with the solution.Also can you please let me know which software do i need to download to run this code?I will be very thankful please help me
Question :-
Given a starting integer n, less than one million, a sequence (which terminates at 1) can be generated using the following criteria;
1. If n is even, n = n/2
2. If n is odd, n = 3n + 1
As an example, if we start with n = 13, the sequence generated would be;
{13, 40, 20, 10, 5, 16, 8, 4, 2 1}
Task: Find the starting integer n which produces the longest sequence.
Note, while the starting integer n must be less than one million, terms in the sequence may be larger than one million, as for example if n = 999,999, the first step will be to set equal to 3n+1, obviously making the next term greater than one million.
Are you saying that as soon as a term is located which is a duplicate we stop searching as well? As you example of a seed of 13 would result in the sequence you have stated then 4:
This is a collatz sequence problem. I have solved this on project euler and the way I did it was to start at the biggest odd number I can find below 1M. This was 999,999; then I knew that to produce a very long sequence, I needed to not include even numbers in the numbers I will be checking because if I do, it will not take very long to reduce that number to 1.
So from 999,999 I checked every odd number below that and this should get you your answer.
As a last tip, I will leave you with this:
The longest sequence is 524
I am very new to C++ i dont even know how to write a simple code but this is my one and only project and the deadline is by tomorrow morning .Now i dont have enough time to learn and work on it .So,can you please copy the code which gets the solution and also can you please let me know which tool do i need to run and compile this code?
oh you poor guy.
is there anything you know about c++ or am I gonna basically write this whole program for you?
pay attention in class? and I think you will need a compiler if you want to get anywhere at all learning c++
*working on solution
Actually i work for a company as a database developer and i don't have any idea on coding but one of my colleague left the company recently and i have now to finish his or else i may be fired from work ...So,can you please help me with the whole code and also tell me what to download to run and complie this code.
> I needed to not include even numbers in the numbers I will be checking because if I do,
> it will not take very long to reduce that number to 1.
That makes no sense.
If the number `k' gives you a path of length `n'
then `2*k' (even) will have length `n+1' (greater)
Given a starting integer n, less than one million, a sequence (which terminates at 1) can be generated using the following criteria;
1. If n is even, n = n/2
2. If n is odd, n = 3n + 1
As an example, if we start with n = 13, the sequence generated would be;
{13, 40, 20, 10, 5, 16, 8, 4, 2 1}
Task: Find the starting integer n which produces the longest sequence.
This, friend, is but a small issue. Here's a solution, or something close to it:
#include <iostream>
usingnamespace std;
int run_sequence(int x);
int main()
{
int largest, terms_ret, terms_max;
terms_max = 0; largest = 0;
for (int quantum=1; quantum < 1000000; quantum++) // this has been edited
{
terms_ret = run_sequence(quantum);
if (terms_ret > terms_max)
{
terms_max = terms_ret;
largest = quantum;
cout << "New longest sequence: " << largest << '\n';
}
}
cout << "The number between 1 and 1000000 which starts the longest sequence is: "
<< largest;
return 0;
}
int run_sequence(int x)
{
int cycle, w;
cycle = 0;
while (x != 1)
{
w = (x % 2);
if (w = 0)
{
x = (x / 2);
}
else
{
x = ((x * 3) + 1);
}
cycle++;
}
return cycle;
}
Now I just wrote that away from home, without a compiler, so if there's anyone following this thread that can plug it in and verify it for me, that'd be just gravy.
This, however:
Actually i work for a company as a database developer and i don't have any idea on coding
is a very very big problem, and that I can't help you so much with.
what all this code we're writing here has to do with your job, I can't imagine, but I would hope if your company expects you to learn to code, they would provide you with a compiler.. shouldn't be too much to ask of them, if they are asking this of you.
> I needed to not include even numbers in the numbers I will be checking because if I do,
> it will not take very long to reduce that number to 1.
trying to find where that is posted originally but,
if you want to skip even numbers just change line 9 in my code ^ to this:for (int quantum=1, quantum < 1000000, quantum = (quantum + 2))good idea, will save time
EDIT
ah but then there's this ^
As a last tip, I will leave you with this:
The longest sequence is 524
guess that means nix on the (quantum + 2) bit
I'm not too hot with math all the times
btw that there ^ is the first number sequence type code I've written, and didn't reference any other example.
I am away from home so if someone could try to compile it that'd be great.. may be errors
any suggestions on how I could improve functionality or pretty-ness of that above code?
You're welcome champ and I hope I'm not coming off as a dick, but
the most important part of my post is AFTER the code block.
I dunno if it will work right or not, honestly, and I do not know where to find you a free c++ compiler. Like I said, if you're going to be doing this as a job, your company ought to provide you with the necessary resources.. sorry, I just don't know where to download it, you may want to make a new post in the beginner forum regarding that.
If you do get your compiler, then you can run my code and see for yerself how well it works. Shouldn't be too far off.. when I get home I'll run it through and check.
Sorry again if I came of as having a bad attitude, but that's the sort of reaction you're likely to get from people when you post a problem without the code (your work).. and again maybe beginner's forum is a better place for this kind of thing
EDIT
Ah yeah I see a problem right now with my above code ^
I'm gonna edit the line with the for loop
If I had a nickel for every time I separated those statements by commas instead of semicolons.. ugh embarassing, my bad.
Thanks once again for your help ....Sorry for troubling you again but if you dont mind can you once run the code and let me know if it works or not after you get back home no hurries...I am being a dick over here but i have no option..And yeah i will post on beginers forum to find a tool to run and compile the code.