Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/kreier/prime
Browse files Browse the repository at this point in the history
  • Loading branch information
kreier committed Dec 18, 2023
2 parents 142a5b4 + 03cf9f5 commit 5fc941d
Show file tree
Hide file tree
Showing 23 changed files with 499 additions and 41 deletions.
2 changes: 1 addition & 1 deletion arduino/v2.0.2022/results_esp8266.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ ESP8266 with 80 MHz
1,000,000 538.199219
10,000,000 14038.631 - 3h 53min 58s
25,000,000 51894.422 - 14h 24min 54s
100,000,000 17h 26% - 32h 43% - 45h 55%
100,000,000 17h 26% - 32h 43% - 45h 55% - 70h 75%
1,000,000,000

9 changes: 5 additions & 4 deletions arduino/v5.0.2023/results_esp8266.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ ESP8266 with 80 MHz
100,000 1.923395
1,000,000 31.340561
10,000,000 610.143433
25,000,000 2069.988381 or 34 min
100,000,000 13579.361 or 3h 46min 19s
1,000,000,000 13h 13% - 16h 29% - 20h 33% - 36h 51%
50h 64% - 63h 77% -
25,000,000 2069.988381 or 34 min
100,000,000 13579.361 or 3h 46min 19s
1,000,000,000 329172.219 or 91h 26min 12s -
2,147,483,647
4,294,967,295

19 changes: 10 additions & 9 deletions arduino/v5.3/prime_logging_ArduinoUno/prime_logging_ArduinoUno.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Prime numbers in Arduino C v5.3 2023/12/15 for Arduino Uno */
// code v5.0 is limited to 10 million for Arduino Uno since we can only store
// less than 750 primes in the limited RAM for this fast algorithm

#include <time.h>
#include <math.h>
#include <EEPROM.h>
Expand All @@ -9,7 +10,7 @@ double start;
int column = 10;
long found = 4; // we already know 2, 3, 5, 7
int divisors = found;
int primes[500] = {3, 5, 7}; // only 447 primes to 3163 = sqrt(10 million)
int primes[672] = {3, 5, 7}; // prime #671 is 5009 > sqrt(25 million)
int led = LED_BUILTIN; // LED_BUILTIN

int is_prime(long number) {
Expand Down Expand Up @@ -69,19 +70,19 @@ void elapsed_time(long seconds) {
}

void setup() {
Serial.begin(115200);
Serial.begin(74880);
pinMode(led, OUTPUT);
for (int i = 0; i < 3; i++) {
Serial.print(".");
delay(1000);
}
const long scope[] = {100, 1000, 10000, 100000, 1000000, 10000000, 25000000, 100000000, 1000000000};
const long reference[] = {25, 168, 1229, 9592, 78498, 664579, 1565927, 5761455, 50847534};
const long scope[] = {100, 1000, 10000, 100000, 1000000, 10000000, 25000000};
const long reference[] = {25, 168, 1229, 9592, 78498, 664579, 1565927};

// previous run
Serial.print("\nGet previous results for this Arduino Uno:\n");
Serial.print("\nPrevious results Arduino Uno:\n");
Serial.print(" last seconds \n");
for(int i = 0; i < 6; i++) {
for(int i = 0; i < 7; i++) {
int spaces = 11 - (int)log10(scope[i]);
for(int j = 0; j < spaces; j++) {
Serial.print(" ");
Expand All @@ -95,12 +96,12 @@ void setup() {
}

// start calculating
for (int i = 0; i < 6; i++) // 9
for (int i = 0; i < 7; i++) // 9
{
long last = scope[i];
found = 4; // we already know 2, 3, 5, 7
Serial.println("\n\nPrime v5.3 in Arduino C - 2023/12/14");
Serial.print("Calculating prime numbers until ");
Serial.println("\n\nPrime v5.3");
Serial.print("Primes until ");
Serial.println(last);
start = millis(); // use micros() for more precision
// Serial.println(start/1000000, 6);
Expand Down
31 changes: 16 additions & 15 deletions arduino/v5.3/prime_logging_Mega2560/prime_logging_Mega2560.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Prime numbers in Arduino C v5.3 2023/12/15 for Mega 2560 */
/* Prime numbers in Arduino C v5.3 2023/12/17 for Mega 2560 */
#include <time.h>
#include <math.h>
#include <EEPROM.h>
Expand All @@ -7,7 +7,7 @@ double start;
int column = 10;
long found = 4; // we already know 2, 3, 5, 7
int divisors = found;
int primes[3500] = {3, 5, 7}; // only 3401 primes to 31623 = sqrt(1 billion)
int primes[3403] = {3, 5, 7}; // only 3402 primes to 31623 = sqrt(1000000000)
int led = LED_BUILTIN; // LED_BUILTIN

int is_prime(long number) {
Expand Down Expand Up @@ -54,9 +54,9 @@ int is_prime_fast(long number) {
}

void elapsed_time(long seconds) {
int hours = (int)seconds/3600;
int minutes = (int)(seconds/60 - hours*60);
int sec = (int)(seconds - minutes*60 - hours*3600);
long hours = (int)seconds/3600;
long minutes = (int)(seconds/60 - hours*60);
long sec = (int)(seconds - minutes*60 - hours*3600);
Serial.print(" ");
Serial.print(hours);
Serial.print("h ");
Expand All @@ -73,35 +73,35 @@ void setup() {
Serial.print(".");
delay(1000);
}
const long scope[] = {100, 1000, 10000, 100000, 1000000, 10000000, 25000000, 100000000, 1000000000};
const long reference[] = {25, 168, 1229, 9592, 78498, 664579, 1565927, 5761455, 50847534};
const uint32_t scope[] = {100, 1000, 10000, 100000, 1000000, 10000000, 25000000, 100000000, 1000000000, 2147483647, 4294967295};
const long reference[] = {25, 168, 1229, 9592, 78498, 664579, 1565927, 5761455, 50847534, 105097564, 203280221};

// previous run
Serial.print("\nGet previous results for this Mega2560:\n");
Serial.print(" last seconds \n");
for(int i = 0; i < 9; i++) {
int spaces = 11 - (int)log10(scope[i]);
for(int i = 0; i < 11; i++) {
int spaces = 12 - (int)log10(scope[i]);
for(int j = 0; j < spaces; j++) {
Serial.print(" ");
}
Serial.print(scope[i]);
Serial.print(" ");
float last_time;
EEPROM.get(i * 4 + 1, last_time);
Serial.print(last_time, 3);
Serial.print(last_time, 6);
Serial.print("\n");
}

// start calculating
for (int i = 0; i < 7; i++) // 9
for (int i = 7; i < 9; i++) // only until 1 billion - not enough memory for more primes
{
long last = scope[i];
found = 4; // we already know 2, 3, 5, 7
Serial.println("\n\nPrime v5.3 in Arduino C - 2023/12/14");
Serial.print("Calculating prime numbers until ");
Serial.println(last);
start = millis(); // use micros() for more precision
// Serial.println(start/1000000, 6);
start = millis();
// start = micros(); // use micros() for more precision in runtimes up to 70 minutes
long largest_divider = (long)(sqrt(last));
if(largest_divider % 2 == 0)
{
Expand Down Expand Up @@ -131,7 +131,7 @@ void setup() {
}
if(column > 40) {
column = 0;
elapsed_time(dot/1000);
elapsed_time((millis()-start)/1000);
Serial.print(" - ");
Serial.print(number);
Serial.print(" ");
Expand All @@ -141,6 +141,7 @@ void setup() {
}
}
const float duration = (millis() - start)/1000;
// const float duration = (micros() - start)/1000000; // use micros for hiher precision
if(duration > 2) {
Serial.print("\n");
}
Expand All @@ -149,7 +150,7 @@ void setup() {
Serial.print(" prime numbers. It should be ");
Serial.print(reference[i]);
Serial.print(".\nThis took ");
Serial.print(duration, 3);
Serial.print(duration, 6);
Serial.print(" seconds.");
elapsed_time(duration);
EEPROM.put(i * 4 + 1, duration);
Expand Down
21 changes: 11 additions & 10 deletions arduino/v5.3/results_Mega2560.txt
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ STM32F411CEU6 with 100 MHz
2,417,483,647
4,294,967,295



4 changes: 4 additions & 0 deletions c/v5.4.2024/100.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/1000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/10000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/100000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/1000000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/10000000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/100000000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/1000000000.txt
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.

4 changes: 4 additions & 0 deletions c/v5.4.2024/25000000.txt
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 added c/v5.4.2024/prime
Binary file not shown.
75 changes: 75 additions & 0 deletions c/v5.4.2024/prime.c
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 added c/v5.4.2024/prime_logging
Binary file not shown.
Loading

0 comments on commit 5fc941d

Please sign in to comment.