falyptus

ProjectEuler.net - Problem 12

May 30th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. /**
  2. *
  3. *@author Keal - Falyptus
  4. */
  5. public static void ResolveProblem12()
  6. {
  7.     long l = System.currentTimeMillis();
  8.     int i = 1, nbr_factor = 0, nb_factor_min = 500, sum = 0;
  9.         while (nbr_factor <= nb_factor_min)
  10.         {
  11.            sum += i;
  12.             nbr_factor = 2;// 0, 1 et lui-même
  13.             double sum_sqrt = Math.sqrt(sum);
  14.             for (int j = 2; j <= sum_sqrt; ++j)
  15.             {
  16.                 if (sum % j == 0)
  17.                 {
  18.                     nbr_factor += 2; //on ajoute la pair de facteur
  19.                 }
  20.             }
  21.             if (sum / (int)sum_sqrt == sum_sqrt)
  22.                 nbr_factor--; //on décrémente de un car les racines carrés sont comptés deux fois
  23.             i++;
  24.         }
  25.     System.out.println("sum: "+sum+"\nnbr_factor: "+nbr_factor+"\nFounded in "+(System.currentTimeMillis()-l)+"ms");
  26. }
Advertisement
Add Comment
Please, Sign In to add comment