| champishere (11) | |
|
Hi, 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. | |
|
|
|
| ajh32 (163) | |
| Before you can run the code you need to write it. Any text editor will do with a compiler, try gcc. | |
|
|
|
| champishere (11) | |
|
Hi Ajh 32, Can you please help me with the solution to this problem? Thanks | |
|
|
|
| ajh32 (163) | |
|
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: {13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4} as 1 being odd would result in 1*3 +1; | |
|
Last edited on
|
|
| Smac89 (201) | |
|
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 gl | |
|
|
|
| champishere (11) | |
|
Hi Ajh 32, The search must stop after we hit 1 so in the above example as soon as we hit 1 we dont need to search again . Please let me know if you have any questions. And if you are clear can you please post the code to get the solution please tomorrow morning is my deadline. Thank you very much for your help. | |
|
|
|
| champishere (11) | |
|
Hi smac89, 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? I will be very thankful to you. | |
|
|
|
| cPlusN00b (92) | |
|
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 | |
|
Last edited on
|
|
| champishere (11) | |
|
Hi cPlus N00b, 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 am very thankful for your help | |
|
|
|
| iHutch105 (1092) | |
|
If you don't have the time to learn and work on it then that's your problem. This is your assignment. Edit: Report this post all you like; it's the truth. | |
|
Last edited on
|
|
| ne555 (4386) | |
|
> 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) | |
|
|
|
| champishere (11) | |
|
Hi ne 555, I clearly didnt understand what you are saying .Can you be more precise? | |
|
|
|
| cPlusN00b (92) | |||||
This, friend, is but a small issue. Here's a solution, or something close to it:
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:
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. | |||||
|
Last edited on
|
|||||
| cPlusN00b (92) | ||
|
lulz @ iHutch reported > 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 for (int quantum=1, quantum < 1000000, quantum = (quantum + 2))
EDIT ah but then there's this ^
guess that means nix on the (quantum + 2) bit I'm not too hot with math all the times edit thanks down there ne555 you sooo right | ||
|
Last edited on
|
||
| ne555 (4386) | |
|
> good idea, will save time It is not, the logic is flawed | |
|
Last edited on
|
|
| cPlusN00b (92) | |
|
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? | |
|
Last edited on
|
|
| champishere (11) | |
|
Hi cPlus N00b, So the code which you gave me above will work fine?Can you also let me know any good tool to work with C++ to write the code and compile it? Thanks a lot for your help. | |
|
|
|
| cPlusN00b (92) | |
|
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. | |
|
Last edited on
|
|
| champishere (11) | |
|
Hi cPlusN00b, 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. Once again thanks a lot | |
|
|
|
| cPlusN00b (92) | |
|
Hey will do, champ. I'm headin home directly and will have code ready for ya later. And you're quite welcome. Sorry that you are in this situation, but tomorrow morning you will have your code and all will be good, eh? I'm on it And if you've got the time, go through this for a bit: http://www.cplusplus.com/doc/tutorial/ You'll probably find it to be enormously helpful. | |
|
Last edited on
|
|