-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/kreier/prime
- Loading branch information
Showing
23 changed files
with
499 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
Calculating prime numbers in Arduino C v5.0.2022 | ||
2023/12/12 | ||
Calculating prime numbers in Arduino C v5.4.2024 | ||
2023/12/17 | ||
Mega2560 with 16 MHz | ||
|
||
last time in seconds | ||
100 0.001136 | ||
1,000 0.014878 | ||
10,000 0.140965 | ||
100,000 | ||
1,000,000 | ||
10,000,000 12524.163 or 3h 28min 44s | ||
25,000,000 42797.867 or | ||
100 0.010732 | ||
1,000 0.111164 | ||
10,000 1.757784 | ||
100,000 31.062923 | ||
1,000,000 598.593505 | ||
10,000,000 12524.163 or 3h 28min 44s | ||
25,000,000 42797.863 or 11h 53min 17s | ||
100,000,000 | ||
1,000,000,000 | ||
|
||
2,147,483,647 nan | ||
4,294,967,295 nan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,5 +15,3 @@ STM32F411CEU6 with 100 MHz | |
2,417,483,647 | ||
4,294,967,295 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 100 in C v5.0.2023 | ||
Found 25 prime numbers. It should be 25. | ||
This took 0.000005 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 1000 in C v5.0.2023 | ||
Found 168 prime numbers. It should be 168. | ||
This took 0.000019 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 10000 in C v5.0.2023 | ||
Found 1229 prime numbers. It should be 1229. | ||
This took 0.000297 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 100000 in C v5.0.2023 | ||
Found 9592 prime numbers. It should be 9592. | ||
This took 0.003766 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 1000000 in C v5.0.2023 | ||
Found 78498 prime numbers. It should be 78498. | ||
This took 0.048427 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 10000000 in C v5.0.2023 | ||
Found 664579 prime numbers. It should be 664579. | ||
This took 0.790443 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 100000000 in C v5.0.2023 | ||
Found 5761455 prime numbers. It should be 5761455. | ||
This took 16.917481 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 1000000000 in C v5.0.2023 | ||
Found 50847534 prime numbers. It should be 50847534. | ||
This took 401.460473 seconds. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Calculating prime numbers until 25000000 in C v5.0.2023 | ||
Found 1565927 prime numbers. It should be 1565927. | ||
This took 2.612616 seconds. | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// prime to 2E31 version 5.4.2024 int32 from 2023/12/16 | ||
|
||
#include <stdio.h> | ||
#include <time.h> | ||
#include <math.h> | ||
#include <stdint.h> | ||
|
||
int32_t primes[6550] = {3, 5, 7}; // to store the 6542 primes to sqr(2E32) | ||
int32_t found = 4; // we already know 2, 3, 5, 7 | ||
|
||
int is_prime(int32_t number) { | ||
int prime = 1; | ||
for (int32_t divider = 3; divider < (int)(sqrt(number)) + 1; divider += 2) | ||
{ | ||
if (number % divider == 0) | ||
{ | ||
prime = 0; | ||
break; | ||
} | ||
} | ||
return prime; | ||
} | ||
|
||
void find_primes(int32_t limit) { | ||
for(int32_t number=11; number < limit + 1; number += 2) { | ||
if( is_prime(number) == 1) { | ||
primes[found - 1] = number; | ||
found++; | ||
} | ||
} | ||
primes[found-1] = limit; | ||
} | ||
|
||
int is_prime_fast(uint32_t number) | ||
{ | ||
uint32_t largest_divider = (uint32_t)(sqrt(number)); | ||
int flag_prime = 1; | ||
for(int i=0; i < found; i++) | ||
{ | ||
if(number % primes[i] == 0) | ||
{ | ||
flag_prime = 0; | ||
break; | ||
} | ||
if(primes[i] >= largest_divider) | ||
{ | ||
break; | ||
} | ||
} | ||
return flag_prime; | ||
} | ||
|
||
int main() | ||
{ | ||
uint32_t last = 10000000; // 4294967295 | ||
clock_t start, end; | ||
double cpu_time_used; | ||
printf("Calculating prime numbers in C until %u with algorithm v5.4.2024\n", last); | ||
start = clock(); | ||
int32_t largest_divider = (int32_t)(sqrt(last)); | ||
if(largest_divider % 2 == 0) | ||
{ | ||
largest_divider += 1; | ||
} | ||
find_primes(largest_divider); | ||
printf("Found %d primes until %d to use as divisors.\n", found, largest_divider); | ||
for(uint32_t number = largest_divider + 2; number < last; number += 2) | ||
{ | ||
found += is_prime_fast(number); | ||
} | ||
end = clock(); | ||
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; | ||
printf("Found %u prime numbers. ", found); | ||
printf("This took %f seconds.\n",cpu_time_used); | ||
} |
Binary file not shown.
Oops, something went wrong.