waltermilner

Untitled

Sep 22nd, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. public class Test {
  2.  
  3.   public static boolean isPrime(int n) {
  4.    .. as before
  5.   }
  6.  
  7.   public static boolean isPrimeHappy(int n) {
  8.     int total = 0;
  9.     // calculate total of primes less than n
  10.     for (int p = 2; p < n; p++) {
  11.       if (isPrime(p)) {
  12.         total += p;
  13.       }
  14.     }
  15.     return (total % n == 0); // does n divide the total?
  16.  
  17.   }
  18.  
  19.   public static void main(String[] args) {
  20.     System.out.println(isPrimeHappy(3));
  21.     System.out.println(isPrimeHappy(5));
  22.     System.out.println(isPrimeHappy(8));
  23.   }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment