From: KingKong
Thats a lot of floors/ceils/extended casts, all you need is a loop where i:= 3 to ceil(sqrt(num)) step 2 and check if num mod i equals 0
So lets see if im getting this right…
checking to ensure its not 2 or 3 goes here. and greater than 4 right? (as that would cause a loop error ie a > b)
ceilsqrt := ceil(sqrt(num))
for i := 3 to ceilsqrt do
begin
result := num mod i = 0
if not result then exit;
end;
(that should be more efficient than my first write at it i believe.
lets try 11.
so ceil(sqrt(11)) = 4
set = [3, 4]
11 mod 3 = 2
11 mod 4 = 3
–> 11 is not prime?
12
set = [3, 4]
12 mod [3, 4] = [0, 0]
13
set = [3, 4]
13 mod [3, 4] = [1, 1]
14
set = [3, 4]
14 mod [3, 4] = [2, 2]
15
set = [3, 4]
15 mod [3, 4] = [0, 3]
skipping ahead…
37
set = [3, 4, 5, 6, 7]
37 mod [3, 4, 5, 6, 7] = [1, 1, 2, 1, 2]
yeah am i doing something wrong here?
okay. wrote a script to do this for me…
program prime;
var
num, i, ends: integer;
arr : TIntegerArray;
begin
for num := 2 to 50 do
begin
writeln(num)
if num > 3 then
begin
ends := ceil(sqrt(num));
setlength(arr, ends - 2);
for i := 3 to ends do
arr[i-3] := num mod i;
writeln(arr);
end
end;
end.
2
3
4
[]
5
[2]
6
[0]
7
[1]
8
[2]
9
[0]
10
[1, 2]
11
[2, 3]
12
[0, 0]
13
[1, 1]
14
[2, 2]
15
[0, 3]
16
[1, 0]
17
[2, 1, 2]
18
[0, 2, 3]
19
[1, 3, 4]
20
[2, 0, 0]
21
[0, 1, 1]
22
[1, 2, 2]
23
[2, 3, 3]
24
[0, 0, 4]
25
[1, 1, 0]
26
[2, 2, 1, 2]
27
[0, 3, 2, 3]
28
[1, 0, 3, 4]
29
[2, 1, 4, 5]
30
[0, 2, 0, 0]
31
[1, 3, 1, 1]
32
[2, 0, 2, 2]
33
[0, 1, 3, 3]
34
[1, 2, 4, 4]
35
[2, 3, 0, 5]
36
[0, 0, 1, 0]
37
[1, 1, 2, 1, 2]
38
[2, 2, 3, 2, 3]
39
[0, 3, 4, 3, 4]
40
[1, 0, 0, 4, 5]
41
[2, 1, 1, 5, 6]
42
[0, 2, 2, 0, 0]
43
[1, 3, 3, 1, 1]
44
[2, 0, 4, 2, 2]
45
[0, 1, 0, 3, 3]
46
[1, 2, 1, 4, 4]
47
[2, 3, 2, 5, 5]
48
[0, 0, 3, 0, 6]
49
[1, 1, 4, 1, 0]
50
[2, 2, 0, 2, 1, 2]
I see no pattern or commonality between the 'output' of primes or composites.