# Tag Archives: perl program

04 Aug

I have a Mathe-crap-matical friend. Who keeps digging into graves of dead formulas and equations and all possible Math related stuffs.
Since I started understanding Mathematics (that it’s not my cup of tea), I tried to stay away from it.

Now, that enthu creature mailed me his requirement. He says he “eat mathematics”, I prefer to stay away from such guys. But as a good friend I read his mails.

His requirement was to create a program. His requirements are:

N is a natural number falling in range [1 – 999999]
X is a number given by user with range [6000 – 20000]

You have to write a program such that you find a number N whose square root or cube root is such that the first digit of X comes in the Xth decimal place and second digit of X comes in the (X+1)th decimal place and so on…

e.g. user enters 12…
then the number N would be such that CubeRoot(N) or SqRoot(N) = dddd.ddddddddddd12

You may write program in any language.

Check screen shot to understand what it does [note the name]:

Alright, I understood the dooms day is closing in… Did you notice the decimal places correction he was asking? 6000!! Damn…

Any ways, I chose perl and accomplished the task. I’m not sure how and why he need this thing but still I hope it rested his soul. 🙂

This post is for people like me who have such math-e-manic friends.

```print "Enter X:";
my \$X = <STDIN>;
\$X=trim(\$X);
my \$N=1;
while (\$N < 100) {
\$SQRT_VAL=\$N**(1/2);
\$CUBERT_VAL=\$N**(1/3);

\$PosAfterDesimal=index (\$SQRT_VAL,'.');
\$PosOfXInSQRT =index (\$SQRT_VAL, \$X,\$PosAfterDesimal);
\$PosOfXInSQRT = \$PosOfXInSQRT -1;
\$PosAfterDesimal=index (\$CUBERT_VAL,'.');
\$PosOfXInCUBERT =index (\$CUBERT_VAL, \$X, \$PosAfterDesimal);
\$PosOfXInCUBERT= \$PosOfXInCUBERT -1;

#	print \$N . "->" . \$SQRT_VAL . "->" . \$X . "->> " .trim(\$PosOfXInSQRT). "\n";
if (trim(\$PosOfXInSQRT) == \$X) {
print \$X. " matched SQRT of " . \$N . " at " . \$X . "th Position \n" ;
print "\t Where, N=" . \$N . ", SQRT VAL=" . \$SQRT_VAL . ", X=" . \$X . "\n";
exit;
} elsif (trim(\$PosOfXInCUBERT) == \$X) {
print \$X. " matched Cube Root of " . \$N . " at " . \$X . "th Position \n";
print "\t Where, N=" . \$N . ", CUBEROOT VAL=" . \$CUBERT_VAL . ", X=" . \$X . "\n";
exit;
}
\$N++;
}
sub trim(\$)
{
my \$string = shift;

if ( \$string eq undef || \$string eq "" )
{
return "";
}
\$string =~ s/^\s+//;
\$string =~ s/\s+\$//;

return \$string;
}#trim Ends```
-- Kedar Vaijanapurkar --