Advertisement
Guest User

Euler OOM

a guest
Mar 24th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.47 KB | None | 0 0
  1. //Rextester.Program.Main is the entry point for your code. Don't change it.
  2. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
  3.  
  4. /**
  5.  
  6.  
  7. 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:
  8.  
  9. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  10.  
  11. By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
  12.  
  13.  
  14. **/
  15.  
  16.  
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text.RegularExpressions;
  21.  
  22. namespace Rextester
  23. {
  24.     public class Program
  25.     {
  26.        
  27.         public static void Main(string[] args)
  28.         {
  29.            new Program();
  30.                            
  31.         }
  32.        
  33.         public Program(){
  34.             double x = 1;
  35.             double sum = 0;
  36.             double findNXHold = findNX(x);
  37.             while(findNXHold <= 55){
  38.                 if(findNXHold % 2 == 0){
  39.                     sum+= findNXHold;
  40.                 }
  41.                 x = x+3;
  42.                 findNXHold = findNX(x);
  43.                
  44.             }
  45.             Console.WriteLine(sum);  
  46.         }
  47.        
  48.         public double findNX(double x){
  49.             if(x == 0){
  50.               return 1;  
  51.             }
  52.             if(x == 1){
  53.              return 2;  
  54.             }
  55.             return 2*findNX(x-2) + findNX(x-3);
  56.         }
  57.        
  58.        
  59.        
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement