## 12 February 2015

### Problem:

Given a positive integer N, print all prime numbers upto N

### Input Format:

positive integer N

### Output Format:

prime numbers from 0 to N in new line

Constraints:
1<=N<=1000000

20

2
3
5
7
11
13
17
19

### Explanations:

Prime numbers upto 20

Solution:

use strict;
use warnings;

chomp(my \$n=<>);
if(\$n>1)
{
print "2\n";
}
my @arr=(2);
for (my \$i=3; \$i<=\$n;\$i+=2) {
my \$isprime = 1;
my \$c = sqrt(\$i) + 1;
foreach my \$p (@arr) {
if (\$p >= \$c) {
last;
}
if (\$i % \$p == 0) {
\$isprime = 0;
last;
}
}
if (\$isprime == 1) {
print "\$i\n";
push(@arr, \$i);
}
}

### Tips:

This algorithms is fastest method to print prime numbers upto 1000000

## 3 February 2015

### Problem:

Given an array of positive integers, display an array after removing all the leading zeros from original array.

### Input Format:

array having elements separated by "space"

### Output Format:

array having elements separated by "space" after removing leading zeros

Constraints:
Each element of array is < 1000000

### Sample Input

007 70 01022 0000000001 00200 20000 0012300

### Sample Output:

7 70 1022 1 200 20000 12300

### Explanations:

Simple, removed all zeros before first non-zero character

Solution:

use strict;
use warnings;

chomp(my \$line=<>);
my @arr=split(" ",\$line);
@arr=map{\$_%1000000}@arr;
print "@arr";

### Tips:

Use mod of maximum value ie 1000000 in our case.

## 1 February 2015

### Problem:

Given a positive integer(n), print square matrix(n x n) in anticlockwise direction.
Check sample input/output for better understanding

### Input Format:

N=a positive integer

### Output Format:

square matrix in anti-clockwise direction

Constraints:
None

4

1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7

### Explanations:

in a matrix, 4 x 4, starting printing numbers from 1 in anticlockwise direction ie down,right,up,left.

Solution:

use strict;
use warnings;

my \$n=<STDIN>;
my \$m=\$n;
my @arr=();
my \$ans=1;
my \$row=0;
my \$col=\$n-1;
my \$i=0;
my \$j=0;
while(\$n>=1)
{
for(\$i=\$row;\$i<=\$col;\$i++)
{
\$arr[\$i][\$row]=\$ans;
\$ans++;
}
for(\$i=\$row+1;\$i<=\$col;\$i++)
{
\$arr[\$col][\$i]=\$ans;
\$ans++;
}
for(\$i=\$col-1;\$i>=\$row;\$i--)
{
\$arr[\$i][\$col]=\$ans;
\$ans++;
}
for(\$i=\$col-1;\$i>=\$row+1;\$i--)
{
\$arr[\$row][\$i]=\$ans;
\$ans++;
}
\$n=\$n-2;
\$row=\$row+1;
\$col=\$col-1;
}
for(\$i=0;\$i<\$m;\$i++)
{
for(\$j=0;\$j<\$m;\$j++)
{
print "\$arr[\$i][\$j] ";
}
print "\n";
}

### Tips:

On completing 1 full anticlockwise cycle, the dimension of matrix will change by 2, so i used \$n=\$n-2