Thread: program hogs

  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
    28,413
    It looks like you forgot to increment index1. You might want to use a for loop instead to make it more obvious.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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 03:44 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    is this algorithm correct ?
    No, since you need to control the loop correctly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 01: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, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM