diff options
| author | hachem <im@hachem.wtf> | 2026-10-04 02:02:15 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-10-04 02:02:15 +0200 |
| commit | 7c1bd03a71cccfcf48e79d241787021b0de98e73 (patch) | |
| tree | b7131a1f4eee60d24781d3f549d191e17793c282 /user/primes.c | |
| parent | 627f05098785327744e666c9149375125499d2ff (diff) | |
feat: add more user programs
Diffstat (limited to 'user/primes.c')
| -rw-r--r-- | user/primes.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/user/primes.c b/user/primes.c new file mode 100644 index 0000000..49c545a --- /dev/null +++ b/user/primes.c @@ -0,0 +1,42 @@ +#include "user.h" + +void _start(void) +{ + put("primes up to N, enter N (empty line quits)\n"); + char line[32]; + for (;;) + { + put("> "); + int length = read_line(line, 32); + put("\n"); + if (length == 0) + break; + + long limit = 0; + for (int position = 0; position < length; position++) + if (line[position] >= '0' && line[position] <= '9') + limit = limit * 10 + (line[position] - '0'); + + int count = 0; + for (long candidate = 2; candidate <= limit; candidate++) + { + int prime = 1; + for (long divisor = 2; divisor * divisor <= candidate; divisor++) + if (candidate % divisor == 0) + { + prime = 0; + break; + } + if (prime) + { + put_int(candidate); + put(" "); + count++; + } + } + put("\n"); + put_int(count); + put(" primes\n"); + } + sys_exit(0); +} |
