interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text sp " "
8text hdr "primes below 100:"
9text yes " is prime\n"
10text no " is not prime\n"
11note emit writes every byte of a to stdout; a short write is retried, an error exits 1
12fn emit a view uchar * to void
13o = ulong 0
14while lt o len a bound 4096
15k = write 1 a o sub len a o
16if le k 0
17exit 1
18end
19o = add o ulong k
20end
21end
22note emitu writes n in decimal; 20 digits hold every ulong
23fn emitu n ulong to void
24d = new uchar 20 0
25i = ulong 20
26v = n
27while true bound 20
28i = sub i 1
29put d i uchar add rem v 10 48
30v = div v 10
31if eq v 0
32break
33end
34end
35emit part d i sub 20 i
36end
37note trial division by 2, then by odd d while d * d <= n
38note 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
39fn prime n ulong to bool
40if lt n 2
41ret false
42end
43if eq rem n 2 0
44ret eq n 2
45end
46d = ulong 3
47while le mul d d n bound 999999
48if eq rem n d 0
49ret false
50end
51d = add d 2
52end
53ret true
54end
55fn tell n ulong to void
56emitu n
57if prime n
58emit yes
59else
60emit no
61end
62end
63fn main to int
64emit hdr
65for i ulong 100
66if prime i
67emit sp
68emitu i
69end
70end
71emit nl
72tell 1
73tell 2
74tell 9
75tell 561
76tell 7919
77tell 2147483647
78tell 999999999989
79tell 999999999999
80tell 1000000000039
81ret 0
82end
83</syntaxhighlight>
84{{out}}
85<pre>
86primes 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
871 is not prime
882 is prime
899 is not prime
90561 is not prime
917919 is prime
922147483647 is prime
93999999999989 is prime
94999999999999 is not prime
951000000000039 is prime
96</pre>