Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. // finding sum of all primes under 2 million
  2. public class lll {
  3. public static void main(String[] args)
  4. {
  5. int a = 2 ;
  6. int b = 0 ;
  7. int c = 0 ;
  8. while (a<2000000)
  9. {
  10. int i = 1;
  11. for (i = 1; i*i<=a; i++)
  12. {
  13. if (a%i == 0)
  14. //if factor is found of number a b goes up 1
  15. {b ++ ;
  16. }
  17. }
  18. // all primes have only one factor <= sqrt (1)
  19. if (b==1)
  20. // if b =1 , a is prime so c+=a
  21. {c += a ;
  22. }
  23. //reset b and add one to a to move on
  24. b = 0 ;
  25. a++ ;
  26. // for error checking see description
  27. System.out.println(c);
  28.  
  29. }
  30. System.out.println(c);
  31. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement