{"id":1016,"date":"2010-08-04T18:02:23","date_gmt":"2010-08-04T18:02:23","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=1016"},"modified":"2014-06-24T17:53:33","modified_gmt":"2014-06-24T17:53:33","slug":"my-worst-mathematical-program","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/my-worst-mathematical-program","title":{"rendered":"My worst mathematical program in perl"},"content":{"rendered":"<p>I have a Mathe-crap-matical friend. Who keeps digging into graves of dead formulas and equations and all possible Math related stuffs.<br \/>\nSince I started understanding Mathematics (that it&#8217;s not my cup of tea), I tried to stay away from it.<\/p>\n<p>Now, that enthu creature mailed me his requirement. He says he &#8220;eat mathematics&#8221;, I prefer to stay away from such guys. But as a good friend I read his mails.<\/p>\n<p>His requirement was to create a program. His requirements are:<\/p>\n<p><em>N is a natural number falling in range [1 &#8211; 999999]<br \/>\nX is a number given by user with range [6000 &#8211; 20000]<\/em><\/p>\n<p><em>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&#8230;<\/em><\/p>\n<p><em>e.g. user enters 12&#8230;<br \/>\nthen the number N would be such that CubeRoot(N) or SqRoot(N) = dddd.ddddddddddd12<\/em><\/p>\n<p><em>You may write program in any language.<\/em><\/p>\n<p>Check screen shot to understand what it does [note the name]:<\/p>\n<figure id=\"attachment_1017\" aria-describedby=\"caption-attachment-1017\" style=\"width: 271px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2010\/08\/maths-program.jpg\"><img decoding=\"async\" class=\"size-medium wp-image-1017\" title=\"maths program\" alt=\"maths program\" src=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2010\/08\/maths-program-271x300.jpg\" width=\"271\" height=\"300\" \/><\/a><figcaption id=\"caption-attachment-1017\" class=\"wp-caption-text\">maths program<\/figcaption><\/figure>\n<p>Alright, I understood the dooms day is closing in&#8230; Did you notice the decimal places correction he was asking? 6000!! Damn&#8230;<\/p>\n<p>Any ways, I chose perl and accomplished the task. I&#8217;m not sure how and why he need this thing but still I hope it rested his soul. \ud83d\ude42<\/p>\n<p>This post is for people like me who have such math-e-manic friends.<br \/>\nYou may download my script <a href=\"http:\/\/kedar.nitty-witty.com\/wp-content\/uploads\/2010\/08\/my_mathematics_pgm.pl_.txt\" target=\"_blank\">my_mathematics_pgm.pl<\/a>.<\/p>\n<pre>print \"Enter X:\";\r\nmy $X = &lt;STDIN&gt;;\r\n$X=trim($X);\r\nmy $N=1;\r\nwhile ($N &lt; 100) {\r\n\t$SQRT_VAL=$N**(1\/2);\r\n\t$CUBERT_VAL=$N**(1\/3);\r\n\r\n\t$PosAfterDesimal=index ($SQRT_VAL,'.');\r\n\t$PosOfXInSQRT =index ($SQRT_VAL, $X,$PosAfterDesimal);\r\n\t$PosOfXInSQRT = $PosOfXInSQRT -1;\r\n\t$PosAfterDesimal=index ($CUBERT_VAL,'.');\r\n\t$PosOfXInCUBERT =index ($CUBERT_VAL, $X, $PosAfterDesimal);\r\n\t$PosOfXInCUBERT= $PosOfXInCUBERT -1;\r\n\r\n#\tprint $N . \"-&gt;\" . $SQRT_VAL . \"-&gt;\" . $X . \"-&gt;&gt; \" .trim($PosOfXInSQRT). \"\\n\";\r\n\tif (trim($PosOfXInSQRT) == $X) {\r\n\t\tprint $X. \" matched SQRT of \" . $N . \" at \" . $X . \"th Position \\n\" ;\r\n\t\tprint \"\\t Where, N=\" . $N . \", SQRT VAL=\" . $SQRT_VAL . \", X=\" . $X . \"\\n\";\r\n\t\texit;\r\n\t} elsif (trim($PosOfXInCUBERT) == $X) {\r\n\t\tprint $X. \" matched Cube Root of \" . $N . \" at \" . $X . \"th Position \\n\";\r\n\t\tprint \"\\t Where, N=\" . $N . \", CUBEROOT VAL=\" . $CUBERT_VAL . \", X=\" . $X . \"\\n\";\r\n\t\texit;\r\n\t}\r\n\t$N++;\r\n}\r\nsub trim($)\r\n{\r\n        my $string = shift;\r\n\r\n        if ( $string eq undef || $string eq \"\" )\r\n        {\r\n                return \"\";\r\n        }\r\n        $string =~ s\/^\\s+\/\/;\r\n        $string =~ s\/\\s+$\/\/;\r\n\r\n        return $string;\r\n}#trim Ends<\/pre>\n","protected":false},"excerpt":{"rendered":"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&#8217;s not my&hellip;\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[24,424,67,227,228,92],"class_list":{"0":"post-1016","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-technical","7":"tag-code","8":"tag-general","9":"tag-mathematics","10":"tag-perl-program","11":"tag-perl-sample-code","12":"tag-program"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=1016"}],"version-history":[{"count":4,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"predecessor-version":[{"id":1941,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/1016\/revisions\/1941"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}