Tag Archives: math

“Presto” math trick in Java

According to Futility Closet, if you start with a 3 digit and place it next to the same number to form a 6 digit number, you can divide the 6 digit number by 7, 11, and then 13 and you will end up with the original 3 digit number and no remainders. For example, by taking the number 412 and making it 412412 and then doing the divisions, you will end up with 412. I wrote a small program in Java to test it.

Read more »

Recursive Collatz conjecture in PHP

I stumbled across a blog post by Brie Gordon about the Collatz conjecture, so naturally I had to Wikipedia it. The simple story is that for any given integer, if it is even you divide it by 2 and if it is odd you multiply it by 3 and add 1. You continue this pattern with the returned numbers, and eventually the number will reach 1. So, to kill some time, I wrote a recursive version in PHP.  Here it is:

function collatz($num)
{   
  if($num == 1)
  {
    echo "<p style='color: red'>" . $num . "</p>n";
    return;
  }
  if($num % 2 == 0) //even
  {
    echo "<p style='color: green'>" . $num . "</p>n";
    return collatz($num / 2);
  }
  else
  {
    echo "<p style='color: red'>" . $num . "</p>n";
    return collatz(3 * $num + 1);
  }
}