"Binary Numbers"
As an example, the integer 5 is represented in binary form by 101. This means, working from right to left:
Given an input file (decimals.txt) of 500 integers, how many 1s IN TOTAL appear in the file in each numbers binary representation?
Say you had the numbers 5, 7 and 10
5 = 101
7 = 111
10 = 1010
Total number of 1s: 7
A fairly simple challenge, download the text file, convert decimals to binary, remove the 0's and count the 1's
Awnser:
= 0
Source:
include(MISC.'fn.fetch.php'); function solution(){ $result = 0; $list = fetch('http://www.cstutoringcenter.com/problems/files/decimals.txt'); $lines = explode("\n", $list); foreach($lines as $decno) { $binno = str_replace("0", "", decbin($decno)); // convert decimal to binary and remove 0's $result += strlen($binno); // length of $binno = no of 1's } echo "= ".$result.""; }