TheQuack45

Project Euler #35 in Java

Mar 11th, 2016
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package circularPrimes;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class CircularPrimes {
  8.  
  9.     public static void main(String[] args) {
  10.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  11.        
  12.         System.out.println("Input number to count circ primes up to: ");
  13.         int n = 0;
  14.         try {
  15.             n = Integer.parseInt(in.readLine());
  16.         } catch (NumberFormatException e1) {
  17.             System.out.println("Please enter a valid number");
  18.             return;
  19.         } catch (IOException e1) {
  20.             System.out.println("Unnamed exception occurred.");
  21.             e1.printStackTrace();
  22.         }
  23.        
  24.         int count = 0;
  25.         for (int i = 2; i < n; i++) {
  26.             if (isCircPrime(i)) {
  27.                 count++;
  28.                 System.out.println(i);
  29.             }
  30.         }
  31.        
  32.         System.out.println("There are " + Integer.toString(count) + " circular primes below " + n);
  33.     }
  34.    
  35.     public static char[] rotateDigits(char[] xArr) {
  36.         char[] rotatedArr = new char[xArr.length];
  37.        
  38.         rotatedArr[rotatedArr.length - 1] = xArr[0];
  39.         for (int i = 0; i <= xArr.length - 2; i++) {
  40.             rotatedArr[i] = xArr[i + 1];
  41.         }
  42.        
  43.         return rotatedArr;
  44.     }
  45.    
  46.     public static boolean isCircPrime(int x)  {
  47.         if (isPrime(x)) {
  48.             // First rotation of digits is prime
  49.             // Continue checking
  50.            
  51.             char[] currentRotation = rotateDigits(Integer.toString(x).toCharArray());
  52.            
  53.             for (int i = 0; i < currentRotation.length - 1; i++) {
  54.                 // Rotate so that each digit rotation is cycled through and checked
  55.                 String currentRotationString = new String(currentRotation);
  56.                 if (!isPrime(Integer.parseInt(currentRotationString))) {
  57.                     // A rotation is not prime
  58.                    
  59.                     return false;
  60.                 }
  61.                
  62.                 currentRotation = rotateDigits(currentRotation);
  63.             }
  64.            
  65.             return true;
  66.         } else {
  67.             return false;
  68.         }
  69.     }
  70.    
  71.     public static boolean isPrime(int x) {
  72.         if (x == 1 || x == 2) {
  73.             return true;
  74.         }
  75.         else if (x % 2 == 0 || x % 5 == 0) {
  76.             return false;
  77.         }
  78.         else {
  79.             for (int i = 3; i < Math.floor(Math.sqrt(x)); i += 2) {
  80.                 if (x % i == 0) {
  81.                     return false;
  82.                 }
  83.             }
  84.            
  85.             return true;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment