Template:Task: Difference between revisions

From Rosetta Code
Content added Content deleted
(corrected semantic markup)
No edit summary
Line 2: Line 2:
You are encouraged to [[Rosetta Code:Solve a Task|solve this task]] according to the task description, using any language you may know.<includeonly>[[Category:Programming Tasks]]{{#if:{{{1|}}}|[[Category:{{{1}}}]]|[[Category:Solutions by Programming Task]]}}</includeonly>{{infobox_end}}<noinclude>
You are encouraged to [[Rosetta Code:Solve a Task|solve this task]] according to the task description, using any language you may know.<includeonly>[[Category:Programming Tasks]]{{#if:{{{1|}}}|[[Category:{{{1}}}]]|[[Category:Solutions by Programming Task]]}}</includeonly>{{infobox_end}}<noinclude>
{{template|Task identifier}}</noinclude>
{{template|Task identifier}}</noinclude>

{{task |Twin-Prime Conjecture}}
A twin prime is a pair of natural numbers P1 and P2 that satisfy the following:

1. P1 and P2 are primes

2. P1 + 2 = P2

Write a program that displays the number of twin primes that can be found under a user-inputted number.

Examples below:
{{out}}
<pre>
> Search Size: 100
> 8 twin prime pairs.
</pre>

<pre>
> Search Size: 1000
> 35 twin prime pairs.
</pre>

=={{header|Java}}==
<lang java>
BigInteger Method:

import java.math.BigInteger;
import java.util.Scanner;

public class twinPrimes {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Search Size: ");
BigInteger max = input.nextBigInteger();
int counter = 0;
for(BigInteger x = new BigInteger("3"); x.compareTo(max) <= 0; x = x.add(BigInteger.ONE)){
BigInteger sqrtNum = x.sqrt().add(BigInteger.ONE);
if(x.add(BigInteger.TWO).compareTo(max) <= 0) {
counter += findPrime(x.add(BigInteger.TWO), x.add(BigInteger.TWO).sqrt().add(BigInteger.ONE)) && findPrime(x, sqrtNum) ? 1 : 0;
}
}
System.out.println(counter + " twin prime pairs.");
}
public static boolean findPrime(BigInteger x, BigInteger sqrtNum){
for(BigInteger divisor = BigInteger.TWO; divisor.compareTo(sqrtNum) <= 0; divisor = divisor.add(BigInteger.ONE)){
if(x.remainder(divisor).compareTo(BigInteger.ZERO) == 0){
return false;
}
}
return true;
}
}
</lang>

<pre>
> Search Size:
> 100
> 8 twin prime pairs.
</pre>

<pre>
> Search Size:
> 1000
> 35 twin prime pairs.
</pre>

Revision as of 05:33, 26 July 2020

Task
Task
You are encouraged to solve this task according to the task description, using any language you may know.

This is a template. There are many others. See Category:RCTemplates for a complete list of templates.
Task
Task
You are encouraged to solve this task according to the task description, using any language you may know.

Template loop detected: Template:Task A twin prime is a pair of natural numbers P1 and P2 that satisfy the following:

1. P1 and P2 are primes

2. P1 + 2 = P2

Write a program that displays the number of twin primes that can be found under a user-inputted number.

Examples below:

Output:
> Search Size: 100
> 8 twin prime pairs.
> Search Size: 1000
> 35 twin prime pairs.

Java

<lang java> BigInteger Method:

import java.math.BigInteger; import java.util.Scanner;

public class twinPrimes {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.println("Search Size: ");
       BigInteger max = input.nextBigInteger();
       int counter = 0;
       for(BigInteger x = new BigInteger("3"); x.compareTo(max) <= 0; x = x.add(BigInteger.ONE)){
           BigInteger sqrtNum = x.sqrt().add(BigInteger.ONE);
           if(x.add(BigInteger.TWO).compareTo(max) <= 0) {
               counter += findPrime(x.add(BigInteger.TWO), x.add(BigInteger.TWO).sqrt().add(BigInteger.ONE)) && findPrime(x, sqrtNum) ? 1 : 0;
           }
       }
       System.out.println(counter + " twin prime pairs.");
   }
   public static boolean findPrime(BigInteger x, BigInteger sqrtNum){
       for(BigInteger divisor = BigInteger.TWO; divisor.compareTo(sqrtNum) <= 0; divisor = divisor.add(BigInteger.ONE)){
           if(x.remainder(divisor).compareTo(BigInteger.ZERO) == 0){
               return false;
           }
       }
       return true;
   }

} </lang>

> Search Size: 
> 100
> 8 twin prime pairs.
> Search Size: 
> 1000
> 35 twin prime pairs.

A twin prime is a pair of natural numbers P1 and P2 that satisfy the following:

1. P1 and P2 are primes

2. P1 + 2 = P2

Write a program that displays the number of twin primes that can be found under a user-inputted number.

Examples below:

Output:
> Search Size: 100
> 8 twin prime pairs.
> Search Size: 1000
> 35 twin prime pairs.

Java

<lang java> BigInteger Method:

import java.math.BigInteger; import java.util.Scanner;

public class twinPrimes {

   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       System.out.println("Search Size: ");
       BigInteger max = input.nextBigInteger();
       int counter = 0;
       for(BigInteger x = new BigInteger("3"); x.compareTo(max) <= 0; x = x.add(BigInteger.ONE)){
           BigInteger sqrtNum = x.sqrt().add(BigInteger.ONE);
           if(x.add(BigInteger.TWO).compareTo(max) <= 0) {
               counter += findPrime(x.add(BigInteger.TWO), x.add(BigInteger.TWO).sqrt().add(BigInteger.ONE)) && findPrime(x, sqrtNum) ? 1 : 0;
           }
       }
       System.out.println(counter + " twin prime pairs.");
   }
   public static boolean findPrime(BigInteger x, BigInteger sqrtNum){
       for(BigInteger divisor = BigInteger.TWO; divisor.compareTo(sqrtNum) <= 0; divisor = divisor.add(BigInteger.ONE)){
           if(x.remainder(divisor).compareTo(BigInteger.ZERO) == 0){
               return false;
           }
       }
       return true;
   }

} </lang>

> Search Size: 
> 100
> 8 twin prime pairs.
> Search Size: 
> 1000
> 35 twin prime pairs.