Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/primality-by-trial-division.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / primality-by-trial-division.wiki`
96 lines ยท 1766 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text sp " "
8 text hdr "primes below 100:"
9 text yes " is prime\n"
10 text no " is not prime\n"
11 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12 fn emit a view uchar * to void
13 o = ulong 0
14 while lt o len a bound 4096
15 k = write 1 a o sub len a o
16 if le k 0
17 exit 1
18 end
19 o = add o ulong k
20 end
21 end
22 note emitu writes n in decimal; 20 digits hold every ulong
23 fn emitu n ulong to void
24 d = new uchar 20 0
25 i = ulong 20
26 v = n
27 while true bound 20
28 i = sub i 1
29 put d i uchar add rem v 10 48
30 v = div v 10
31 if eq v 0
32 break
33 end
34 end
35 emit part d i sub 20 i
36 end
37 note trial division by 2, then by odd d while d * d <= n
38 note bound: the largest input is below 10^12 + 40, so d stays below 10^6 + 2 (at most 500001 odd values) and mul d d does not wrap
39 fn prime n ulong to bool
40 if lt n 2
41 ret false
42 end
43 if eq rem n 2 0
44 ret eq n 2
45 end
46 d = ulong 3
47 while le mul d d n bound 999999
48 if eq rem n d 0
49 ret false
50 end
51 d = add d 2
52 end
53 ret true
54 end
55 fn tell n ulong to void
56 emitu n
57 if prime n
58 emit yes
59 else
60 emit no
61 end
62 end
63 fn main to int
64 emit hdr
65 for i ulong 100
66 if prime i
67 emit sp
68 emitu i
69 end
70 end
71 emit nl
72 tell 1
73 tell 2
74 tell 9
75 tell 561
76 tell 7919
77 tell `2147483647`
78 tell `999999999989`
79 tell `999999999999`
80 tell `1000000000039`
81 ret 0
82 end
83
84 {{out}}
85
86 primes below 100: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
87 1 is not prime
88 2 is prime
89 9 is not prime
90 561 is not prime
91 7919 is prime
92 `2147483647` is prime
93 `999999999989` is prime
94 `999999999999` is not prime
95 `1000000000039` is prime
96