Skip to content

Instantly share code, notes, and snippets.

@Show-vars
Last active August 29, 2015 14:07
Show Gist options
  • Save Show-vars/35600619d29a7b78beb7 to your computer and use it in GitHub Desktop.
Save Show-vars/35600619d29a7b78beb7 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <ctime>
#define N_MAX 100000000
int main() {
int n = 4;
double f = 1.0;
long k = 1;
long startTime = clock();
for(; k < N_MAX; k++) {
f *= pow(1.0 + (1.0 / k), n) / (1.0 + ((double) n / k));
}
long stopTime = clock();
printf("%d! = %f in %d ms (%d iterations)\n", n, f, stopTime - startTime, k);
return 0;
}
public class NumberFact {
public static void main(String[] args) {
int n = 4;
double f = 1.0;
long k = 1;
long startTime = System.currentTimeMillis();
for (; k < 100_000_000; k++) {
f *= Math.pow(1.0 + (1.0 / k), n) / (1.0 + ((double) n / k));
}
long endTime = System.currentTimeMillis();
System.out.println(n + "! = " + f + " in " + (endTime - startTime) + " ms (" + k + " iterations)");
}
}
<?php
$n = 4.0;
$f = 1.0;
$k = 1;
$startTime = round(microtime(true) * 1000);
for (; $k < 100000000; $k++) {
$f *= pow(1.0 + (1.0 / $k), $n) / (1.0 + ($n / $k));
}
$endTime = round(microtime(true) * 1000);
echo $n . "! = " . $f . " in " . ($endTime - $startTime) . " ms (" . $k . " iterations)\n";
?>
Java: 4! = 23.999998473795994 in 12827 ms (100000000 iterations)
C++: 4! = 23.999999 in 23280 ms (100000000 iterations)
PHP: 4! = 23.999998473786 in 23602 ms (100000000 iterations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment