Find the smallest twins in given range
Given a range [low..high], print the smallest twin numbers in given range (low and high inclusive). Two numbers are twins if they are primes and there difference is 2.
Example:
Input: low = 10, high = 100
Output: Smallest twins in given range: (11, 13)
Both 11 and 13 are prime numbers and difference
between them is two, therefore twins. And these
are the smallest twins in [10..100]
Input: low = 50, high = 100
Output: Smallest twins in given range: (59, 61)
A Simple Solution is to start to start from low and for every number x check if x and x + 2 are primes are not. Here x varies from low to high-2.
An Efficient Solution is to use Sieve of Eratosthenes
1) Create a boolean array "prime[0..high]" and initialize all
entries in it as true. A value in prime[i] will finally
be false if i is not a prime number, else true.
2) Run a loop from p = 2 to high.
a) If prime[p] is true, then p is prime. [See this]
b) Mark all multiples of p as not prime in prime[].
3) Run a loop from low to high and print the first twins
using prime[] built in step 2.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
void printTwins( int low, int high)
{
bool prime[high+1], twin = false ;
memset (prime, true , sizeof (prime));
prime[0] = prime[1] = false ;
for ( int p=2; p<= floor ( sqrt (high))+1; p++)
{
if (prime[p])
{
for ( int i=p*2; i<=high; i += p)
prime[i] = false ;
}
}
for ( int i=low; i<=high; i++)
{
if (prime[i] && prime[i+2])
{
cout << "Smallest twins in given range: ("
<< i << ", " << i+2 << ")" ;
twin = true ;
break ;
}
}
if (twin == false )
cout << "No such pair exists" <<endl;
}
int main()
{
printTwins(10, 100);
return 0;
}
|
Java
class GFG {
static void printTwins( int low, int high) {
boolean prime[] = new boolean [high + 1 ], twin = false ;
for ( int i = 0 ; i < prime.length; i++) {
prime[i] = true ;
}
prime[ 0 ] = prime[ 1 ] = false ;
for ( int p = 2 ; p <= Math.floor(Math.sqrt(high)) + 1 ; p++) {
if (prime[p]) {
for ( int i = p * 2 ; i <= high; i += p) {
prime[i] = false ;
}
}
}
for ( int i = low; i <= high; i++) {
if (prime[i] && prime[i + 2 ]) {
int a = i + 2 ;
System.out.print( "Smallest twins in given range: ("
+ i + ", " + a + ")" );
twin = true ;
break ;
}
}
if (twin == false ) {
System.out.println( "No such pair exists" );
}
}
public static void main(String[] args) {
printTwins( 10 , 100 );
}
}
|
Python3
import math
def printTwins(low, high):
prime = [ True ] * (high + 1 );
twin = False ;
prime[ 0 ] = prime[ 1 ] = False ;
for p in range ( 2 , int (math.floor(
math.sqrt(high)) + 2 )):
if (prime[p]):
for i in range (p * 2 , high + 1 , p):
prime[i] = False ;
for i in range (low, high + 1 ):
if (prime[i] and prime[i + 2 ]):
print ( "Smallest twins in given range: (" ,
i, "," , (i + 2 ), ")" );
twin = True ;
break ;
if (twin = = False ):
print ( "No such pair exists" );
printTwins( 10 , 100 );
|
C#
using System;
public class GFG {
static void printTwins( int low, int high) {
bool []prime = new bool [high + 1]; bool twin = false ;
for ( int i = 0; i < prime.Length; i++) {
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for ( int p = 2; p <= Math.Floor(Math.Sqrt(high)) + 1; p++) {
if (prime[p]) {
for ( int i = p * 2; i <= high; i += p) {
prime[i] = false ;
}
}
}
for ( int i = low; i <= high; i++) {
if (prime[i] && prime[i + 2]) {
int a = i + 2 ;
Console.Write( "Smallest twins in given range: ("
+ i + ", " + a + ")" );
twin = true ;
break ;
}
}
if (twin == false ) {
Console.WriteLine( "No such pair exists" );
}
}
public static void Main() {
printTwins(10, 100);
}
}
|
PHP
<?php
function printTwins( $low , $high )
{
$prime = array_fill (0, $high + 1, true);
$twin = false;
$prime [0] = $prime [1] = false;
for ( $p = 2; $p <= floor (sqrt( $high )) + 1; $p ++)
{
if ( $prime [ $p ])
{
for ( $i = $p * 2; $i <= $high ; $i += $p )
$prime [ $i ] = false;
}
}
for ( $i = $low ; $i <= $high ; $i ++)
{
if ( $prime [ $i ] && $prime [ $i + 2])
{
print ( "Smallest twins in given range: ($i, " .
( $i + 2). ")" );
$twin = true;
break ;
}
}
if ( $twin == false)
print ( "No such pair exists\n" );
}
printTwins(10, 100);
?>
|
Javascript
<script>
function printTwins(low, high)
{
var prime = Array.from({length: high + 1}, (_, i) => 0);
var twin = false ;
for (i = 0; i < prime.length; i++)
{
prime[i] = true ;
}
prime[0] = prime[1] = false ;
for (p = 2;
p <= Math.floor(Math.sqrt(high)) + 1;
p++)
{
if (prime[p])
{
for (i = p * 2; i <= high; i += p)
{
prime[i] = false ;
}
}
}
for (i = low; i <= high; i++)
{
if (prime[i] && prime[i + 2])
{
var a = i + 2 ;
document.write( "Smallest twins in " +
"given range: (" + i +
", " + a + ")" );
twin = true ;
break ;
}
}
if (twin == false )
{
document.write( "No such pair exists" );
}
}
printTwins(10, 100);
</script>
|
Output:
Smallest twins in given range: (11, 13)
Thanks to Utkarsh Trivedi for suggesting this solution.
Last Updated :
11 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...