Jump to content

If it will help anybody - I will run few some of these Euler problems I posted in my rant the oth...


G+_Nate Follmer
 Share

Recommended Posts

If it will help anybody - I will run few some of these Euler problems I posted in my rant the other day and add a bit of explanation (code is commented also). 

 

Figured I'd pick problem 2 to start, because starting with problem 1 would be too mainstream :P (Actually I can go back and do that one if anyone doesn't know how). 

 

Here's the problem: https://projecteuler.net/problem=2

 

If you're lost, don't panic - let's break this down into workable parts.

 

1. We obviously need our code to generate a Fibonacci sequence. This will involve some repetition, so we need some kind of loop. 

 

2. We need some code to figure out if the current value we are working with is even. 

 

3. We need some code to keep a running sum if our previous code finds this number to be true. 

 

Easy!

 

1. The Fibonacci Sequence - This can be broken down into even smaller parts:

 

     a. We need to get the 'current value' - let's call this j.

     b. We need to get the 'next value' - let's call this k.

     c. We need to get the sum of these two values, so we know the 'next next' value - let's call this sum.

 

Actually, I don't like how this problem is worded - a true Fibonacci sequence would be 1,1,2,3,5... where the problem says 1,2,3,5...

 

Knowing that, we already have our current and next values, so we can set variables j and k to 1. I always initialize my variables to 0 if I don't have a value to start with... Not necessary, but it's an old habit. 

 

int sum = 0;

int j = 1;

int k = 1;

 

Some other variables I created:

 

int upperlimit = 4000000;

int evensum = 0;

 

upperlimit is how far we want our sequence to go (this will 'stop' our loop)

evensum adds together all of the even values in the sequence 

 

Still with me? :)

 

Ok, there are a few different ways you could actually generate the Fibonacci sequence, but I decided to use a while loop. I just find them a lot cleaner and would be easy to read for new-comers. 

 

while(j <= upperlimit)

{

     Console.WriteLine (j);

     sum=j+k;

     j=k;

     k=sum;

}

 

This might get a bit confusing, so stick with me here! It might even be a good idea to set a breakpoint on the 'while' line and watch the values of j, k and sum, so you can see what's going on. 

 

As long as j is less than or equal to 4 million, the code in this loop will run. Next, we output j (our current value) to the console. sum is then set equal to our current and next value (j and k). Next, we actually set j equal to the next value in the sequence (k). Then, set k equal to the NEW 'next' number in the sequence by setting k equal to the sum of the older values of j and k (held by our sum variable). 

 

See, not much code to create a Fibonacci sequence, just takes a bit of pre-planning. 

 

Now onto part 2 - figure out if our current value is even. 

 

if (j % 2 == 0) {

     Console.WriteLine (j + " " + "*");

     evensum += j;

} else {

     Console.WriteLine (j);

}

 

% <- this little guy here is the Modulus Operator - it gives you the remainder of division. So, if j would equal 5, 5%2 is 1. This is how you check if a number is even. If there is a remainder when dividing by 2, you have an odd number. I added some code to star that value in our output, so we could manually check our program was working as needed. We then add the current value of evensum with the current value of j. += is the same as saying evensum = evensum + j. The next line simply outputs the odd values of the sequence. 

 

Ok, guess I explained part 2 and 3 there... Over-achiever. 

 

Did you notice something cool? Every 3rd number is even. That's got to be science or something at work. 

 

Now, I have a challenge for all the new-comers - Below is a link to the code on GitHub. Edit the program so it will generate and add the even values of the sequence all the way up to 4,000,000,000. I'll give you a hint... It's easy, but it's not as easy as simply changing the upperlimit variable :) You might notice Int will stop at a certain value. 

 

Bonus points if you can generate the Fibonacci sequence with a different loop or even create your own function and call it in Main. 

 

Winner gets... well, I don't have anything awesome to give out, so you gain knowledge and maybe I'll put your name on the GitHub page. Easy now! 

   

https://github.com/Geek-Fish/Fibonacci-Even-Sums/blob/master/Program.cs

Link to comment
Share on other sites

 Share

×
×
  • Create New...