1 |
198 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # < Computes and prints prime numbers > #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # Computes all primes numbers between 3 and 2^32-1 using the harderr #
|
5 |
|
|
// # approximation way ;) The printed numbers are formated using sprintf from #
|
6 |
|
|
// # the C std library <stdio.h> #
|
7 |
|
|
// # ********************************************************************************************* #
|
8 |
|
|
// # BSD 3-Clause License #
|
9 |
|
|
// # #
|
10 |
|
|
// # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
11 |
|
|
// # #
|
12 |
|
|
// # Redistribution and use in source and binary forms, with or without modification, are #
|
13 |
|
|
// # permitted provided that the following conditions are met: #
|
14 |
|
|
// # #
|
15 |
|
|
// # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
16 |
|
|
// # conditions and the following disclaimer. #
|
17 |
|
|
// # #
|
18 |
|
|
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of #
|
19 |
|
|
// # conditions and the following disclaimer in the documentation and/or other materials #
|
20 |
|
|
// # provided with the distribution. #
|
21 |
|
|
// # #
|
22 |
|
|
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to #
|
23 |
|
|
// # endorse or promote products derived from this software without specific prior written #
|
24 |
|
|
// # permission. #
|
25 |
|
|
// # #
|
26 |
|
|
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS #
|
27 |
|
|
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF #
|
28 |
|
|
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE #
|
29 |
|
|
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
|
30 |
|
|
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
|
31 |
|
|
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED #
|
32 |
|
|
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
|
33 |
|
|
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED #
|
34 |
|
|
// # OF THE POSSIBILITY OF SUCH DAMAGE. #
|
35 |
|
|
// # ********************************************************************************************* #
|
36 |
|
|
// # The NEO430 Processor - https://github.com/stnolting/neo430 #
|
37 |
|
|
// #################################################################################################
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
// Libraries
|
41 |
|
|
#include <stdint.h>
|
42 |
|
|
#include <neo430.h>
|
43 |
|
|
|
44 |
|
|
// Configuration
|
45 |
|
|
#define BAUD_RATE 19200
|
46 |
|
|
|
47 |
|
|
/* ------------------------------------------------------------
|
48 |
|
|
* INFO Main function
|
49 |
|
|
* ------------------------------------------------------------ */
|
50 |
|
|
int main(void) {
|
51 |
|
|
|
52 |
|
|
// setup UART
|
53 |
|
|
neo430_uart_setup(BAUD_RATE);
|
54 |
|
|
|
55 |
|
|
// intro text
|
56 |
|
|
neo430_printf("\n\nGenerating prime numbers between 3 and %n", 0xFFFFFFFF);
|
57 |
|
|
neo430_printf(".\n"
|
58 |
|
|
"Press any key to start.\n"
|
59 |
|
|
"You can abort the program by pressing any key again.\n");
|
60 |
|
|
|
61 |
|
|
// wait for any key
|
62 |
|
|
while(!neo430_uart_char_received());
|
63 |
|
|
|
64 |
|
|
uint32_t n = 0, i = 0;
|
65 |
|
|
uint8_t is_prime = 0;
|
66 |
|
|
|
67 |
|
|
// generate candidates
|
68 |
|
|
for (n=3; n!=0xFFFFFFFF; n+=2) {
|
69 |
|
|
|
70 |
|
|
// check if prime
|
71 |
|
|
is_prime = 1;
|
72 |
|
|
for (i=2; i<=(n>>1); i++) {
|
73 |
|
|
if (n%i == 0) {
|
74 |
|
|
is_prime = 0;
|
75 |
|
|
break;
|
76 |
|
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
// output prime number in decimal representation
|
80 |
|
|
if (is_prime)
|
81 |
|
|
neo430_printf("%n, ", n);
|
82 |
|
|
|
83 |
|
|
// abort?
|
84 |
|
|
if (neo430_uart_char_received()) // any key input?
|
85 |
|
|
neo430_soft_reset();
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
return 0;
|
89 |
|
|
}
|