Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static boolean isPrime(int n) {
- .. as before
- }
- public static boolean isPrimeHappy(int n) {
- int total = 0;
- // calculate total of primes less than n
- for (int p = 2; p < n; p++) {
- if (isPrime(p)) {
- total += p;
- }
- }
- return (total % n == 0); // does n divide the total?
- }
- public static void main(String[] args) {
- System.out.println(isPrimeHappy(3));
- System.out.println(isPrimeHappy(5));
- System.out.println(isPrimeHappy(8));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment