Saturday, July 5, 2014

Prime generator

Generate all prime numbers between two given numbers!(problem from spoj.com)

Input

The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output

For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5
 
MY CODE: 
 
#!/usr/bin/perl
use strict;
use warnings; 
print "Enter the number of times:";
$a=<STDIN>;
chomp($a);
for($b=0;$b<$a;$b++)
{
print "Enter the starting number:";
my $x=<STDIN>;
chomp($x);
print "Enter the end range:";
my $y=<STDIN>;
chomp($y);
print "$x ","$y\n";
for(my $j=$x;$j<=$y;$j++)
{
my $c=0;

for(my $i=1;$i<=$j;$i++)
{
if($j % $i ==0)
{
$c++;
#print $c;
}
}
if($c<=2&&$j!=1)
{
print "$j\n";
}
}
}
 
 
 
stdin 
1
1
10 
 
stdout 
2
3
5
7
 

No comments:

Post a Comment