-3

Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are twin primes. Write a Java program TwinPrimes.java that prompts the user to input the search range of twin primes, display all the twin primes (2 pairs per line) within the range, and print the number of twin primes found. The search range is assumed to be positive and your program should repeatedly perform the same task until a sentinel value of -1 is entered.

The expected output of your program should be as follows:

Round 1: Enter the search range: 100

(3,5) (5,7)

(11,13) (17,19)

(29,31) (41,43)

(59,61) (71,73)

Number of twin primes less than or equal to 100 is 8

Round 2: Enter the search range: 150

(3,5) (5,7)

(11,13) (17,19)

(29,31) (41,43)

(59,61) (71,73)

......(Omitted)

Number of twin primes less than or equal to 200 is 15

Round 4:

Enter the search range: -1

End

I know that I am not complected the code, but I am struggling on how to print the Prime numbers in ( , ) ( , )way and how to calculate the number of twin primes show it at the end.

The below coding is what I had to do:

import java.util.Scanner;

import java.util.Random;

public class TwinPrimes {

    public static void main(String[] args) {

        int i = 0;

        int A, B = 0, D = 0;

        int num = 0;

        System.out.println("Round" + " " + ++i + ":");
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter the search range:");
        A = scn.nextInt();
        {
            if (A < 0)
                System.out.println("End");
        }

        for (i = 3; i <= A; i++) {

            int counter = 0;
            for (num = B; num >= 1; num--) {

                {
                    if (B % num == 0) {
                        counter = counter + 1;
                    }
                }
                if (counter == 2) {

                }
            }
            System.out.println("(" + i + "," + i + ")" + " " + "(" + i + "," + i + ")");

            // sum Number of twin primes to
            System.out.println("Number of twin primes less than or equal to " + A + " " + "is" + " ");
            return;
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
hthoo2008
  • 3
  • 1
  • 2

2 Answers2

0

I am struggling on how to print the Prime numbers in ( , ) ( , ) way

You may use String.format. See How to use String.format in Java?

For example, String.format("Found pair (%d,%d)", prime1, prime2);

How to calculate the number of twin primes show it at the end.

Simply keep a counter, say int counter when you're looking for the prime pairs.


import java.util.Scanner;
public class Main {
    // Consider writing a helper function to check primality
    public static boolean isPrime(int n) {
        if (n <= 1) return false;
        if (n == 2) return true;
        for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int max_number = 100;
        int counter = 0; // keep a count
        for (int i = 2; i+2 < max_number; i++) {
          if (isPrime(i) && isPrime(i+2)) {
            counter++; // increment counter
            String msg = String.format("Found pair (%d,%d)", i, i+2);
            System.out.println(msg);
          }
        }
        System.out.println("Total number of pairs is " + counter);
        // Total number of pairs is 8
    }
}
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
0
package array1;

import java.io.IOException;
import java.util.Scanner;

public class Mainclass {
    public static void main(String args[]) throws NumberFormatException, IOException
    {
        int a,k=0,line=0,count=0;
        while(true)
        {
        System.out.println("Round" + " " + ++k + ":");
            Scanner scn = new Scanner(System.in);
            System.out.print("Enter the search range:");
            a= scn.nextInt();
            {
                if (a< 0)
                {
                    System.out.println("End");
                System.exit(0);
                }
            }
            System.out.println("The Twin Prime Numbers within the given range are : ");
            for(int i=2; i<=(a-2); i++)
            {
                if(isPrime(i) == true && isPrime(i+2) == true)
                {
                    System.out.print("("+i+","+(i+2)+") ");
                    line++;
                    if(line==2)
                    {
                        System.out.println();
                        line=0;
                    }
                    count++;

                }

            }
            System.out.println();
            System.out.println("the number of twin prime numbers less than or equal to"+a+"is"+count);
        }
    }

     static boolean isPrime(int n) //funton for checking prime
     {
         int count=0;
         for(int i=1; i<=n; i++)
             {
                 if(n%i == 0)
                     count++;
             }
         if(count == 2)
             return true;
          else
             return false;
     }
}
shiv keerthi
  • 32
  • 13
  • Thank you so much, I try to understand it. – hthoo2008 Oct 24 '17 at 05:15
  • In this coding, "The number of twin prime numbers less than or equal to 150 is 19." That is wrong, it should be 11. – hthoo2008 Oct 24 '17 at 06:01
  • Add a line count=0 after System.out.println("the number of twin prime numbers less than or equal to"+a+"is"+count); that will solve your problem – shiv keerthi Oct 24 '17 at 06:13
  • We should use "int" to remark user input, then, while (input >0) to run this programme. Before the while loop, we can handle the 'if (A == -1) { System.out.println("End");' to end the programme – hthoo2008 Oct 24 '17 at 06:29
  • Yeah that's also a way , different brains different ideas – shiv keerthi Oct 24 '17 at 06:32
  • "Add a line count=0 after System.out.println("the number of twin prime....." Is it means int count = 0 again? – hthoo2008 Oct 24 '17 at 06:49
  • Count is keeping track of the number of twin primes within the given range.So before the next round we need clear the count of previous round if not the count of twin prime from last round is added to this round – shiv keerthi Oct 24 '17 at 07:03
  • No not Int count again just count=0 it'll rewrite the count value – shiv keerthi Oct 24 '17 at 07:04
  • System.out.println()System.out.println("the number of twin prime numbers less than or equal to"+a+"is"+count); count=0; } – shiv keerthi Oct 24 '17 at 07:07
  • try after adding the line i told you u"ll get the correct output – shiv keerthi Oct 24 '17 at 07:07