Skip to content

Instantly share code, notes, and snippets.

@fintara
Created January 20, 2014 18:33
Show Gist options
  • Save fintara/8526152 to your computer and use it in GitHub Desktop.
Save fintara/8526152 to your computer and use it in GitHub Desktop.
CodeEval competition - Predict The Number. Thanks to ferhatelmas/algo for the idea! Very interesting solution.
<?php
$input = file_get_contents($argv[1]);
$rows = explode("\n", $input);
foreach($rows as $row) {
if($row >= 0 && $row <= 3000000000) {
$bin = decbin($row);
$sum = 0;
for($i = 0; $i < strlen($bin); $i++)
if($bin[$i] == "1") $sum++;
echo $sum % 3 . "\n";
}
}
@samarthbhargav
Copy link

How does this work? I'm baffled!

@ac-tech-madcoz
Copy link

@samarthbhargav: the fomula can be searched from the OEIS, this link https://oeis.org/A071858

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment