Alright so...the assignment is;
(Emirp) An emirp (prime spelled backwards) is a non palindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are emrips. Write a program that displays the first 100 emrips. Display 10 numbers per line, separated by exactly one space as follows:
13 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
With the following changes included by the teacher:
Allow the user to determine the actual numbers of emirps to display rather than a fixed 100 emrips. The emrips are displayed 10 per line with each emrip displayed within the space of 7 characters. The user should have the capability to continue generating emrips until a non negative number is entered.
My current code is:
import java.util.Scanner; public class Project3_5_27 { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); System.out.print("Enter number of desired emirps: "); int emrips = scanner.nextInt(); int count = 1; for( int i = 2; ; i++){ if ((isPrime(i)) && (isPrime(reverseIt(i))) && (!isPalindrome(i))) { System.out.print(i + " "); if (count % 10 == 0) { System.out.println(); } if (count == emrips){ break; } count++; } } } public static boolean isPrime(int num){ for (int i = 2; i <=num / 2; i++){ if (num % i == 0) { return false; } if ((num == 1) || (num == 2)){ return true; } } return true; } public static int reverseIt(int num){ int result = 0; while (num != 0) { int lastDigit = num % 10; result = result * 10 + lastDigit; num /= 10; } return result; } public static boolean isPalindrome(int num){ return num == reverseIt(num); } }
I'm having issues figuring out how to implement the two underlined changes made by my teacher...I'm thinking that he means that each paired emrip must be within 7 characters of each other in the out.print
I'm currently getting higher numbers that are way more than 7 character apart ex:
and.................................................. .............1847 1867
1879 1901 1913 1933 1949 1979 3011 3019 3023 3049
3067 3083 3089 3109 3121 3163 3169 3191 3203 3221
3251 3257 3271 3299 3301 3319 3343 3347 3359 3371
3373 3389 3391 3407 3433 3463 3467 3469 3511 3527
3541 3571 3583 3613 3643 3697 3719 3733 3767 3803
3821 3851 3853 3889 3911 3917 3929 7027 7043 7057
7121 7177 7187 7193 7207 7219 7229 7253 7297 7321
7349 7433 7457 7459 7481
instead of completing the process once the desired emrips are generated the program would ask for additional emrips until a non negative number is entered...which I don't get the non negative part, it's not making sense in my mind...as the only things we are dealing with are non negative numbers...
If anyone has ideas to offer, It would be much appreciated.
Thanks