#Problem Definition: #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. print "Enter X:"; my $X = ; $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