|
-
April 24th, 2009, 05:44 PM
#1
Project Euler #14
I'm working on the project Euler series in my free time for fun, and right now, I'm on problem #14. The specs of the project can be found here: http://projecteuler.net/index.php?se...problems&id=14.
I'm doing this in C++, and here is my sourcecode:
Code:
#include <iostream>
#include <windows.h>
using namespace std;
void main()
{ unsigned long int answer=1,length=0,test,temp,cycle=0;
for(test=1;test<1000000;test++)
{ temp=test;
while(temp>1)
{
if(temp%2==0)
{ temp=temp/2;
++cycle;}
else{
temp=(3*temp)+1;
++cycle;}
}
if(cycle>length)
{length=cycle;
answer=test;}
}
cout<<"number: "<<answer<<" length: "<<length+1<<"\n"; //add 1 to include '1' in the sequence, since it was excluded from the while loop
system("pause");
}
However, this incorrectly returns the answer of 999999 and a length of 131435088. I review my code several times, and don't see what is the problem.
-
April 24th, 2009, 10:27 PM
#2
Re: Project Euler #14
 Originally Posted by shadowx360
Code:
#include <iostream>
#include <windows.h>
using namespace std;
void main()
{ unsigned long int answer=1,length=0,test,temp,cycle=0;
for(test=1;test<1000000;test++)
{ temp=test;
while(temp>1)
{
if(temp%2==0)
{ temp=temp/2;
++cycle;}
else{
temp=(3*temp)+1;
++cycle;}
}
if(cycle>length)
{length=cycle;
answer=test;}
}
cout<<"number: "<<answer<<" length: "<<length+1<<"\n"; //add 1 to include '1' in the sequence, since it was excluded from the while loop
system("pause");
}
Non-standard rubbish. 
 Originally Posted by shadowx360
I review my code several times, and don't see what is the problem.
You need to reset cycle at every iteration in your for-loop.
My solution is analogous to yours:
Code:
#include <iostream>
int main()
{
unsigned int result = 0U;
for(unsigned int index = 3U, maximumSteps = 0U; index < 1000000U; index += 2U)
{
unsigned int stepCount = 0U;
for(unsigned int currentPosition = index; currentPosition > 1U; ++stepCount)
{
currentPosition = (currentPosition & 1U) ? (currentPosition * 3U + 1U) : (currentPosition / 2U);
}
if(stepCount > maximumSteps)
{
maximumSteps = stepCount;
result = index;
}
}
std::cout << result << std::endl;
}
Last edited by Plasmator; April 24th, 2009 at 10:44 PM.
Reason: Typo
-
April 25th, 2009, 11:25 AM
#3
Re: Project Euler #14
Thank you for your reply, I figured this out last night, and I forgot to reset the cycle value to 0 in the beginning of the 'for' loop.
However, as for the #include <windows.h> and system("pause"); I needed a way to pause the program at the end for the readout, and I know I can use getche(), but I would have to use conio.h for that. Is there a better alternative using just <iostream>?
Also, as for "void" main, since your code didn't have a return value, VS2008's compiler would automatically convert the "int main()" into "void main()" when it doesn't find a return value in the code. But I guess it is better practice to do so?
-
April 25th, 2009, 11:57 AM
#4
Re: Project Euler #14
 Originally Posted by shadowx360
However, as for the #include <windows.h> and system("pause"); I needed a way to pause the program at the end for the readout, and I know I can use getche(), but I would have to use conio.h for that. Is there a better alternative using just <iostream>?
Yes. Run the application from the command line -- as it's meant to be run. Set a breakpoint at the end if you feel lazy, at the very least.
Besides, system lives in the standard cstdlib header, not windows.h (which just happens to include it -- probably stdlib.h, though).
 Originally Posted by shadowx360
Also, as for "void" main, since your code didn't have a return value, VS2008's compiler would automatically convert the "int main()" into "void main()" when it doesn't find a return value in the code. But I guess it is better practice to do so?
I don't know where you got that information, but it's plain wrong. main has only two standard signatures:
int main();
int main(int, char**);
Read carefully: You are not required to explicitly return anything from main. In fact, if no return statement is found by the time that control reaches the end of main's scope, you are guaranteed that the effects will be as if an implicit return 0 was executed. End of story.
-
April 25th, 2009, 12:11 PM
#5
Re: Project Euler #14
 Originally Posted by shadowx360
Is there a better alternative using just <iostream>?
Alternatively, you can use:
Code:
std::cout << "Press any key to exit.";
std::cin.get();
 Originally Posted by shadowx360
Also, as for "void" main, since your code didn't have a return value, VS2008's compiler would automatically convert the "int main()" into "void main()" when it doesn't find a return value in the code. But I guess it is better practice to do so?
I doubt that VS2008 would actually alter your code. I don't have it on my pc right now, so I can't check, but I never heard of a compiler altering the code it is told to compile.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
April 25th, 2009, 12:19 PM
#6
Re: Project Euler #14
 Originally Posted by D_Drmmr
Alternatively, you can use:
Code:
std::cout << "Press any key to exit.";
std::cin.get();
Artificially pausing your program is a horrible habit. Besides, in order for that to properly work, you'd need to discard any left overs in the stream before calling get.
 Originally Posted by D_Drmmr
I doubt that VS2008 would actually alter your code. I don't have it on my pc right now, so I can't check, but I never heard of a compiler altering the code it is told to compile.
As I said before, there is nothing magical going on. In fact, in order to comply with the standard, all the compiler has to do if no explicit return statement is found is "mov eax, 0" or "xor eax, eax" just before ret-ing.
That's it.
Last edited by Plasmator; April 25th, 2009 at 12:21 PM.
-
April 25th, 2009, 01:17 PM
#7
Re: Project Euler #14
Thank you to everyone for your replies. I learned C++ from an online tutorial, and I guess they didn't include lots of the basics. As for the int main() part, this warning is returned when int main() doesn't return a value: "warning C4508: 'main' : function should return a value; 'void' return type assumed." Thanks to everyone for clearing it up for me.
-
April 25th, 2009, 01:22 PM
#8
Re: Project Euler #14
 Originally Posted by shadowx360
As for the int main() part, this warning is returned when int main() doesn't return a value: "warning C4508: 'main' : function should return a value; 'void' return type assumed."
...Which should tell you heaps about that version's conformance to the standard. Upgrade to something newer, if possible.
-
April 26th, 2009, 04:22 AM
#9
Re: Project Euler #14
 Originally Posted by shadowx360
As for the int main() part, this warning is returned when int main() doesn't return a value: "warning C4508: 'main' : function should return a value; 'void' return type assumed."
A quick check of MSDN's documentation for warning C4508 shows that that warning is issued when you declare a function without specifying the return type (and apparently error C4430 should be disabled, otherwise it would be an error instead of just a warning).
Consequently, I find it hard to believe that C4508 would be issued with this sample program:
 Originally Posted by Plasmator
...Which should tell you heaps about that version's conformance to the standard. Upgrade to something newer, if possible.
That would mean switching to a different compiler vendor entirely since the VS2008 compiler is the most standard conformant Microsoft compiler currently released. Frankly, I think that this is not a problem with the compiler. This is a problem with the code that shadowx360 tested.
Last edited by laserlight; April 26th, 2009 at 04:24 AM.
-
April 26th, 2009, 10:59 AM
#10
Re: Project Euler #14
 Originally Posted by laserlight
That would mean switching to a different compiler vendor entirely since the VS2008 compiler is the most standard conformant Microsoft compiler currently released. Frankly, I think that this is not a problem with the compiler. This is a problem with the code that shadowx360 tested.
I suggested upgrading because I was under the impression that shadowx360 confused VC++ 9 with an older version.
In fact, IIRC, this sort of "extension" was removed several years ago in an earlier version (I think it was 7).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|