Initial commit
This commit is contained in:
6
C/Makefile
Normal file
6
C/Makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
compile:
|
||||
gcc code_cycle.c -O3 -o code_cycle
|
||||
gcc code_unoptimised.c -O3 -o code_unoptimised
|
||||
|
||||
clean:
|
||||
rm code_cycle code_unoptimised
|
||||
BIN
C/code_cycle
Executable file
BIN
C/code_cycle
Executable file
Binary file not shown.
27
C/code_cycle.c
Normal file
27
C/code_cycle.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int iterations = 256*256*256;
|
||||
int successes_amount = 0;
|
||||
|
||||
for (int a = 0; a < 256; a++)
|
||||
{
|
||||
for (int b = 0; b < 256; b++)
|
||||
{
|
||||
for (int c = 0; c < 256; c++)
|
||||
{
|
||||
if (a + b + c > 300)
|
||||
{
|
||||
successes_amount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Iterations: %d\n", iterations);
|
||||
printf("Valid sums: %d\n", successes_amount);
|
||||
printf("Probability: %f\n", (float) successes_amount / iterations);
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
C/code_unoptimised
Executable file
BIN
C/code_unoptimised
Executable file
Binary file not shown.
33
C/code_unoptimised.c
Normal file
33
C/code_unoptimised.c
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define TOTAL_ATTEMPTS 15000000
|
||||
|
||||
int get_random_bit()
|
||||
{
|
||||
int r = rand() % 256;
|
||||
return r;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int successful_attempts = 0;
|
||||
|
||||
for (int i = 0; i < TOTAL_ATTEMPTS; i++)
|
||||
{
|
||||
int a = get_random_bit();
|
||||
int b = get_random_bit();
|
||||
int c = get_random_bit();
|
||||
|
||||
if (a + b + c > 300)
|
||||
{
|
||||
successful_attempts++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Iterations: %d\n", TOTAL_ATTEMPTS);
|
||||
printf("Valid sums: %d\n", successful_attempts);
|
||||
printf("Probability: %lf\n", (double) ((double) successful_attempts / (double) TOTAL_ATTEMPTS));
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user