-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem70.Mod
92 lines (87 loc) · 2.08 KB
/
problem70.Mod
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
MODULE problem70;
IMPORT Out;
VAR
phi: ARRAY 10000001 OF REAL;
i: INTEGER;
j: INTEGER;
min: INTEGER;
minratio: REAL;
value: INTEGER;
ratio: REAL;
PROCEDURE IntToString(n: INTEGER; VAR arr: ARRAY OF CHAR; pos: INTEGER);
BEGIN
IF n # 0 THEN
IntToString(n DIV 10, arr, pos - 1);
arr[pos] := CHR(n MOD 10 + ORD("0"));
END;
END IntToString;
PROCEDURE Sort(VAR arr: ARRAY OF CHAR; size: INTEGER);
VAR
k0: INTEGER;
k1: INTEGER;
minpos: INTEGER;
tmp: CHAR;
BEGIN
FOR k0 := 0 TO size - 1 DO
minpos := k0;
FOR k1 := k0 + 1 TO size - 1 DO
IF arr[k1] < arr[minpos] THEN
minpos := k1;
END;
END;
tmp := arr[minpos];
arr[minpos] := arr[k0];
arr[k0] := tmp;
END;
END Sort;
PROCEDURE IsPerm(n: INTEGER; m: INTEGER): BOOLEAN;
VAR
narr: ARRAY 8 OF CHAR;
marr: ARRAY 8 OF CHAR;
iter: INTEGER;
final: BOOLEAN;
BEGIN
FOR iter := 0 TO 7 DO
narr[iter] := ".";
marr[iter] := ".";
END;
IntToString(n, narr, 7);
IntToString(m, marr, 7);
Sort(narr, 8);
Sort(marr, 8);
final := TRUE;
FOR iter := 0 TO 7 DO
IF narr[iter] # marr[iter] THEN
final := FALSE;
END;
END;
RETURN final
END IsPerm;
BEGIN
FOR i := 2 TO 10000000 DO
phi[i] := FLT(i);
END;
FOR i := 2 TO 10000000 DO
IF phi[i] = FLT(i) THEN
j := i;
WHILE j <= 10000000 DO
phi[j] := phi[j] * (1.0 - 1.0 / FLT(i));
j := j + i;
END;
END;
END;
min := 0;
minratio := 99999999.0;
FOR j := 2 TO 10000000 DO
value := FLOOR(phi[j]);
IF IsPerm(value, j) THEN
ratio := FLT(j) / FLT(value);
IF ratio < minratio THEN
min := j;
minratio := ratio;
END;
END;
END;
Out.Int(min, 8);
Out.Ln();
END problem70.