File: gen_primes.lpr

package info (click to toggle)
castle-game-engine 6.4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 194,520 kB
  • sloc: pascal: 364,585; ansic: 8,606; java: 2,851; objc: 2,601; cpp: 1,412; xml: 851; makefile: 725; sh: 563; php: 26
file content (55 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{ Generate table of primes for IntegerNoise.
  Output Pascal code with $1 rows of primes table.
  In each row, each prime number will be randomly chosen from appropriate
  range (hardcoded in Ranges[] here).
}

uses SysUtils, CastleUtils;

function IsPrime(const Value: Cardinal): boolean;
var
  I: Cardinal;
begin
  for I := 2 to IntSqrt(Value) do
    if Value mod I = 0 then Exit(false);
  Result := true;
end;

function RandomPrime(const Min, Max: Cardinal): Cardinal;
begin
  Result := Min + Random(Max - Min);
  while not IsPrime(Result) do
  begin
    Inc(Result);
    if Result > Max then Result := Min;
  end;
end;

const
  { For 5 columns of Primes[] table in IntegerNoise, ranges from where
    to choose random prime number.
    Sizes of these consts just chosen to match the "good" numbers from
    Blender's source code: 1301 314159, 15731, 789221, 1376312589. }
  Ranges: array [0..4] of array [0..1] of Cardinal =
  (
    (       1000,        2000),
    (     200000,      800000),
    (      10000,       20000),
    (     500000,     5000000),
    ( 1000000000,  2000000000)
  );
var
  I, J: Cardinal;
begin
  Parameters.CheckHigh(1);
  for I := 1 to StrToInt(Parameters[1]) do
  begin
    Write('(');
    for J := 0 to 4 do
    begin
      Write(RandomPrime(Ranges[J, 0], Ranges[J, 1]));
      if J <> 4 then Write(', ');
    end;
    Writeln('),');
  end;
end.