Sunday, July 6, 2014

Life, the universe and everything

Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example

Input:
1
2
88
42
99

Output:
1
2
88 
 
My Code: 
$c=1;
chomp($input=<>);
while ($input ne "")
{
if($input>99)
{
exit;
}
@array[$c-1]=$input;
$c++;
chomp($input=<>);
}


$i=0;
while(@array[$i]!=42)
{
if($i!=$c)
{
print "$array[$i]\n";
}
$i++;
}


INPUT:
1
2
45
65
78
42
34
56
68
42
56
78
98

OUTPUT:
1
2
45
65
78

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