Problem:
Find if a number is a perfect square without using inbuilt functions like sq root
Sample Input:
999998000001
Sample Output:
999998000001 is a perfect square
Solution:
print "Enter the number\n";
$num = <STDIN>;
chomp($num);
$a = 1;
$b = $num;
$tmp=0;
while($b > 0)
{
$b = $b-$a;
$a = $a + 2;
if($b == 0)
{
print "$num is a Perfect square";
$tmp++;
}
}
if($tmp == 0)
{
print "$num is not a perfect square";
}
Tips:
Find if a number is a perfect square without using inbuilt functions like sq root
Sample Input:
999998000001
Sample Output:
999998000001 is a perfect square
Solution:
print "Enter the number\n";
$num = <STDIN>;
chomp($num);
$a = 1;
$b = $num;
$tmp=0;
while($b > 0)
{
$b = $b-$a;
$a = $a + 2;
if($b == 0)
{
print "$num is a Perfect square";
$tmp++;
}
}
if($tmp == 0)
{
print "$num is not a perfect square";
}
Tips:
- Try to understand the logic by taking a smaller value like 64 and finding a and b in while loop.
No comments:
Post a Comment