Posts

Showing posts from October, 2016

Shortest Code For Generating Prime Numbers

<?php for ($i = 1; $i < 1000; $i++) {     $print = TRUE;        // Optimized till square roots     for ($j = 2; ($j*$j) <= $i; $j++)     {         $print = FALSE;         if ($i % $j == 0)             break;                $print = TRUE;     }     if ($print)         print $i.','; } ?>

The Essence of Computer Programming

Computers are basically for the automation of repetitive work. So programming computers is essentially the art of automation of repetitive work using loops. Thus, the most essential thing one needs to understand to program computers is how to write loops. That covers everything essential. PS: Recursion is assumed here to just be a more advanced form of a loop, which is just a more elegant solution for certain types of problems.

C Code for Numerology 9999

Got a strange request from a friend: Generate all 4 digit numbers whose recursive sums evaluate to nine, and whose non-zero digits are in ascending order. Anyway, here's the code. First run as usual :-) Apart from a few syntax corrections. No logical errors. <?php for ($i = 9; $i < 10000; $i++) {     if (isAsc($i))     {         if (isSumNine($i))         {             if ($i < 10)                 print '000'.$i.PHP_EOL;             else if ($i < 100)                 print '00'.$i.PHP_EOL;             else if ($i < 1000)                 print '0'.$i.PHP_EOL;             else                 print $i.PHP_EOL;         }     } } function isSumNine($num) {     $temp = $num;     $sum = 0;     while ($temp > 0)     {         $sum += $temp % 10;         $temp = floor($temp / 10);     }     if ($sum > 9)         return isSumNine($sum);     else         return ($sum == 9); } function isAsc($num)

Easiest Way To Run Code on Mac OSX

Most links will tell you to install XCode so that you can use GCC to run C / C++ code. You don't need to. The easiest way is to use the existing PHP program on your Mac OSX.