Yesterday I watched a nice (short) video about a mathematics problem with the number '33'. Link: https://www.youtube.com/watch?v=wymmCdLdPvM
In short: Many numbers can be calculated by: a³ + b³ + c³
For example: 29 = 3³ + 1³ + 1³
(should be mentioned: a
, b
, c
can be negative!)
So, I though I give the problem a short bruteforce try. Nothing sophisticated, just to see how fast I can compute those values.
I built 3 Programs (PHP, Pyhton and C, let's concentrate on PHP and C here), and thats what I've got...
PHP-Program:
#!/usr/bin/php5
<?php
$total = 0;
$aktstamp = time();
for ($a = -100000000000; $a <= 100000000000; $a++) {
for ($b = -100000000000; $b <= 100000000000; $b++) {
for ($c = -100000000000; $c <= 100000000000; $c++) {
$total++;
if ($a**3+$b**3+$c**3 == 33) {
echo "FOUND IT! a**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ((-$a)**3+$b**3+$c**3 == 33) {
echo "FOUND IT! (-a)**3+$b**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ($a**3+(-$b)**3+$c**3 == 33) {
echo "FOUND IT! a**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ($a**3+$b**3+(-$c)**3 == 33) {
echo "FOUND IT! a**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ((-$a)**3+(-$b)**3+$c**3 == 33) {
echo "FOUND IT! (-a)**3+(-$b)**3+$c**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ((-$a)**3+$b**3+(-$c)**3 == 33) {
echo "FOUND IT! (-a)**3+$b**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ($a**3+(-$b)**3+(-$c)**3 == 33) {
echo "FOUND IT! a**3+(-$b)**3+(-$c)**3 a: " . $a . " b: " . $b + " c: " . $c;
}
if ($total % 10000000 == 0) {
$timetaken = time() - $aktstamp;
$calcspersec = 10000000 / $timetaken;
$date = date("d.m.Y", time());
$time = date("H:i:s", time());
echo $date . " " . $time . ": " . $a . "\n";
echo "(Calcs per sec: " . $calcspersec . ")\n";
$aktstamp = time();
}
}
}
}
?>
And a C Program:
/*
Compile with:
gcc ./33.c -o 33 -lm -O1
*/
#include <stdio.h>
#include <math.h>
#include <sys/time.h>
long long main(long long argc, char *argv[])
{
long long total = 0, a = 0, b = 0, c = 0;
int timetaken, calcspersec;
int aktstamp = (int)time(NULL);
for (a = -100000000000; a <= 100000000000; a++) {
for (b = -100000000000; b <= 100000000000; b++) {
for (c = -100000000000; c <= 100000000000; c++) {
total++;
if (pow(a, 3)+pow(b, 3)+pow(c, 3) == 33) {
printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(-a, 3)+pow(b, 3)+pow(c, 3) == 33) {
printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
printf("FOUND IT! pow(a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(-a, 3)+pow(-b, 3)+pow(c, 3) == 33) {
printf("FOUND IT! pow(-a, 3)+pow(-b, 3)+pow(c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(-a, 3)+pow(b, 3)+pow(-c, 3) == 33) {
printf("FOUND IT! pow(-a, 3)+pow(b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (pow(a, 3)+pow(-b, 3)+pow(-c, 3) == 33) {
printf("FOUND IT! pow(a, 3)+pow(-b, 3)+pow(-c, 3) a: %lld b: %lld c: %lld\n", a, b, c);
}
if (total % 10000000 == 0) {
timetaken = (int)time(NULL) - aktstamp;
calcspersec = 10000000 / timetaken;
printf("%lld\n", a);
printf("(Calcs per sec: %u)\n", calcspersec);
aktstamp = time(NULL);
}
}
}
}
}
Here are the Speeds:
PHP: (Calcs per sec: 2000000)
Python: (Calcs per sec: 1186440)
C: (Calcs per sec: 833333)
It was no suprise to me that PHP was faster than Python. But why on earth is C so much slower than PHP?
Even Python is faster than C...
Do I have a stupid error, or is the C-pow() Function so much slower than PHPs **
?
So, does anyone see a obvious reason for why C is half as fast as PHP?