CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2008
    Posts
    68

    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.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Project Euler #14

    Quote Originally Posted by shadowx360 View Post
    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&#37;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.

    Quote Originally Posted by shadowx360 View Post
    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

  3. #3
    Join Date
    Oct 2008
    Posts
    68

    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?

  4. #4
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Project Euler #14

    Quote Originally Posted by shadowx360 View Post
    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).

    Quote Originally Posted by shadowx360 View Post
    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.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Project Euler #14

    Quote Originally Posted by shadowx360 View Post
    Is there a better alternative using just <iostream>?
    Alternatively, you can use:
    Code:
    std::cout << "Press any key to exit.";
    std::cin.get();
    Quote Originally Posted by shadowx360 View Post
    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

  6. #6
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Project Euler #14

    Quote Originally Posted by D_Drmmr View Post
    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.

    Quote Originally Posted by D_Drmmr View Post
    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.

  7. #7
    Join Date
    Oct 2008
    Posts
    68

    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.

  8. #8
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Project Euler #14

    Quote Originally Posted by shadowx360 View Post
    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Project Euler #14

    Quote 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:
    Code:
    int main(){}
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Project Euler #14

    Quote Originally Posted by laserlight View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured