top of page


/*
(Twin primes) 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 program to find all twin primes less than 1,000. Display the output as follows:
(3, 5)
(5, 7)
...
*/

package Calismalarim;

public class twinPrimes {
    
    public static void main(String[] args) {

        final byte Number_of_primes = 100;
        final byte Number_of_per_line = 10;
        long starttime=System.currentTimeMillis();

        int number = 2;
        int count = 0;
        int PrimeNumbers = 1;

        while (PrimeNumbers <= 100) {
            if (isPrime(number)&&isPrime(number+2)) {
                PrimeNumbers++;
                if (PrimeNumbers % Number_of_per_line == 0)
                    System.out.println(" ("+number +" , "+(number+2)+" )");
                else
                    System.out.print(" ("+number +" , "+(number+2)+" )");
            }
            number++;
        }
        
        long endtime=System.currentTimeMillis();
        System.out.println("gecen süre "+(endtime-starttime)+" milis");
        
    }


    public static boolean isPrime(int number) {
        boolean isPrime = true;
        for (int i = 2; i <= number / 2; i++) {
            if (number % i == 0) {
                isPrime = false;
                break;
            } else
                isPrime = true;
        }
        return isPrime;
    }
}
 

bottom of page