yandex
loader

please wait

  • Ayushi Jain Feb-12-2019 05:52:33 AM ( 1 month ago )

     

    Write a program to read n numbers. The first number specified as input will be n. Next, the program should read n integer numbers.

     

    The program should check for each number if it is prime as well as if its reverse is prime.

    Display all such numbers in ascending order.

    Consider below example for input and output:

    Input: 7 11 12 23 19 7 113 101

    Output:

    7 11 101 113

    My code

    public class Prime {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int x = sc.nextInt();
            int temp;
    
            int[] a = new int [x];
            int[] r = new int [x];
            int[]c = new int[a.length+r.length];
            int[] rev = new int [x];
    
            for(int i=0;i<x;i++){
                a[i] = sc.nextInt();
                rev[i]=a[i];
            }
    
            for(int i = 0; i < a.length; i++) {
                while(rev[i] != 0) {
                    r[i] = r[i] * 10;
                    r[i] = r[i] + rev[i]%10;
                    rev[i] = rev[i]/10;
                }
            }
    
            for(int i = 0; i < a.length; i++) {
                boolean isPrime = true;
                for (int j = 2; j < i; j++) {
                    if((
  • Vikrant Srivastava Feb-12-2019 05:57:22 AM ( 1 month ago )

    To eliminate duplicates , use a Set

  • Katie George Feb-12-2019 05:58:33 AM ( 1 month ago )

    Please show what you have tried, and why exactly it didn't work, in the smallest amount of code possible. We're here to help you solve a specific problem, not write your code for you.

  • Arminder Gill Feb-12-2019 06:02:52 AM ( 1 month ago )

    Try breaking down the problem into smaller chunks. I recommend making a few methods which each handle a small portion of the program. You can then test each of them individually. First make a method to check if a number is prime boolean isPrime(int i), then make method to reverse the number, int reverse(int i). Create a method boolean isReversePrime(int i) which calls both isPrime(i) and isPrime(reverse(i)), etc etc. Breaking down a problem into smaller steps may look like more work, but it makes your code much easier to read and also much easier to verify the correctness.

  • Nageshwer Reddy Feb-12-2019 06:04:32 AM ( 1 month ago )

    Instead of checking till n, we can check till √n because a larger factor of n must be a multiple of smaller factor which all ready cover.

  • Apoorva Saxena Feb-12-2019 06:05:50 AM ( 1 month ago )

    You need to use TreeSet - which will contain only distinct elements and give result in sorted form. You can refer to following code-

    Set<Integer> set = new TreeSet<>();
            for(int i = 0; i < a.length; i++) {
                boolean isPrime = true;
                if(isPrime(a[i]) && isPrime(r[i]))
                    set.add(a[i]);
            }

    Also create a function for checking prime numbers -

    private static boolean isPrime(int num) {
            for(int i = 2; i <= num/2; ++i)
            {
                if(num % i == 0)
                {
                   return false;
                }
            }
            return true;
        }
  • Sophia Chang Feb-12-2019 06:08:34 AM ( 1 month ago )

    Here is the code for prime test using √n approach

    static boolean isPrime(int n){
    //corner case
    if (n <= 1) return false; 
    if (n <= 3) return true; 
    //middle 5 number
    if (n % 2 == 0 || n % 3 == 0) return false;
    for (int i = 5; i * i <= n; i = i + 6) 
                if (n % i == 0 || n % (i + 2) == 0) 
                return false;  
    return true;
    }

    Use can use set for remove duplicate element

  • Lucky Negi Feb-12-2019 06:10:10 AM ( 1 month ago )

    You can use Stream.distinct() in Java 8 just pass your array to an array list and remove its duplicates by using .distinct()

    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.List; 
    import java.util.stream.Collectors; 
    
            //list with duplicates 
            List<Integer> list = new ArrayList<>( 
                Arrays.asList(3, 10, 3, 3, 4, 5, 5)); 
            //new list without duplicates
            List<Integer> newList = list.stream() 
                                          .distinct() 
                                          .collect(Collectors.toList()); 

Please login

Similar Discussion

Recommended For You