Guest User

Untitled

a guest
Sep 15th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.87 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. // Problème Euler 2
  4.  
  5. void main(){
  6.  
  7. immutable uint MAX = 4_000_000;
  8. uint[] tableau = new uint[] (5);
  9. tableau[0] = 1;
  10. tableau[1] = 2;
  11. uint somme = 0;
  12. // On va mettre ici la séquence des termes
  13.  
  14. ushort i = 1, j;  // On va commencer à tableau[2] quand même ;)
  15.  
  16.     while(tableau[i] < MAX - tableau[i-1]){ // Sans la soustraction, on dépasse le MAX
  17.    
  18.         if(i == tableau.length-1) // -1 car après on accède à tableau[i+1] qui existe pas encore
  19.             tableau.length *= 2;
  20.        
  21.         tableau[i+1] = tableau[i-1] + tableau[i];
  22.         //somme += tableau[i+1];
  23.         i++;    
  24.        
  25.     }
  26.    
  27.     foreach(ligne; tableau[])
  28.         writeln(ligne);
  29.        
  30.     writeln("---------");
  31.  
  32.     for(i = 1; i <= tableau.length-2; i+=2 )
  33.         somme += tableau[i];
  34.  
  35.  
  36.     write( "Somme des nombres pairs : " , somme);
  37.  
  38. }
Add Comment
Please, Sign In to add comment