Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need a little help with an assignment (emrips)

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need a little help with an assignment (emrips)

    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:

    .................................................. .............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
    and

    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
    Last edited by ChuChuTrick; October 3rd, 2012 at 08:54 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need a little help with an assignment (emrips)

    ...until a non negative number is entered
    This suggests to use a negative number to generate emirps and a non negative to quit, but how does one get a negative number of emirps? I would drop the word non, and go with enter a positive value to get the number of emirps, then the program does the work and displays the results. Now you have to enter a number again to either repeat the process by using another positive number, or quit the program by entering a negative number.

    I read the instructions to mean "format the length of each emirp to 7 characters" such that:
    13 17 31 37 71 73 79 97 107 113
    looks more like:
    -----13 -----17 -----31 -----37 -----71 -----73 -----79 -----97 ----107 ----113
    with spaces rather than the - used here for clarity.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need a little help with an assignment (emrips)

    ...
    if ((isPrime(i)) && (isPrime(reverseIt(i))) && (!isPalindrome(i))) { //isPalindrome looks like less work, use short-circuit evaluation to do the heavy lifting only when the light work is valid.
    ...
    if ((num == 1) || (num == 2)){//This check does not need to be done every iteration of the loop.
    ...

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need a little help with an assignment (emrips)

    Thank you for your insight, your analysis of "format the length of each emirp to 7 characters" seems much more accurate...I should be able to implement all the changes, thanks for the help
    Last edited by ChuChuTrick; October 4th, 2012 at 12:19 PM.

Similar Threads

  1. Assignment
    By millions in forum Java Theory & Questions
    Replies: 1
    Last Post: July 2nd, 2012, 04:15 PM
  2. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  3. Assignment please Help :(
    By pagalas07 in forum Collections and Generics
    Replies: 3
    Last Post: March 30th, 2011, 08:52 AM
  4. please help me in my assignment :(
    By asdfg in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 18th, 2010, 07:59 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 08:20 AM