program hogs

This is a discussion on program hogs within the C++ Programming forums, part of the General Programming Boards category; I solving the problems at Project Euler I'm at problem 2 and I have made this program so far but ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    program hogs

    I solving the problems at Project Euler

    I'm at problem 2

    and I have made this program so far

    but I dont know why it takes too much time to give me the answer
    even if the fibonacci number is 4

    the code is this

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	typedef unsigned long ul;
    
    	ul n1 = 0;
    	ul n2 = 1;
    	ul n3;
    	ul index1 = 0;
    	long double sum = 0;
    	while (index1 < 4) {
    		n3 = n1 + n2;
    		if ( n3 % 2 == 0 ) { sum += n3; }
    		n1 = n2;
    		n2 = n3;
    	}
    	cout << sum;
    
    	return EXIT_SUCCESS;
    }
    any hint on how to improve would be really appreciated

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    It looks like you forgot to increment index1. You might want to use a for loop instead to make it more obvious.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    thanks and is this algorithm correct ?

    i have to find the solution for this
    Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

    Find the sum of all the even-valued terms in the sequence which do not exceed one million.


    here's the new code

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    	typedef long double ul;
    
    	ul n1 = 0;
    	ul n2 = 1;
    	unsigned long n3;
    	ul sum = 0;
    	for (ul index = 0; index < 1000000; index++) {
    	    n3 = n1 + n2;
    	    if ((n3 &#37; 2) == 0 ) { sum += n3; }
    	    n1 = n2;
    	    n2 = n3;
    	}
    
    	cout << fixed << sum;
    	return EXIT_SUCCESS;
    }
    Last edited by manzoor; 02-28-2008 at 02:44 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    is this algorithm correct ?
    No, since you need to control the loop correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    ??? what do you mean is the loop still controlled in corecctly ?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Oh, so you updated your post.

    Well, you need to keep looping until you reach the largest even valued term less than a million. At the moment your loop loops a million times, which is different.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    164
    oh thanks < 30 worked

    thanks laserlight FTW

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Although it may work, it is not an entirely correct solution. What would you do, if you were required to find the sum of even fibonacci numbers up to 2 000 000. Or a user-input value?

    You should not be looping a certain number of times, but as long - for example - n2 meets certain conditions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21