Basic algorithm - Number

xiaoxiao2021-03-06  103

First, the number of algorithms

1. Seeking two maximum number of conventions

Function GCD (A, B: Integer): Integer;

Begin

IF b = 0 THEN GCD: = a

Else GCD: = GCD (B, A MOD B);

END;

2. Seeking two minimum common multiple

Function LCM (A, B: Integer): Integer;

Begin

IF a

LCM: = A;

While LCM MOD B> 0 DO INC (LCM, A);

END;

3. Justice

A. Decree a number in a small range is the number:

Function Prime (N: Integer): Boolean;

VAR i: integer;

Begin

For i: = 2 to trunc (SQRT (N)) DO

IF n mod i = 0 THEN BEGIN

PRIME: = false; exit;

END;

PRIME: = TRUE;

END;

B. Judging whether the number within the longint range is the number of prime numbers (including the number of prime tables within 500,000):

Procedure getprime;

VAR

i, j: longint;

P: array [1..50000] of boolean;

Begin

Fillchar (P, SizeOf (P), TRUE);

P [1]: = false;

i: = 2;

While i <5000 do begin

IF P [i] THEN Begin

J: = i * 2;

While J <50000 Do Begin

P [J]: = FALSE

INC (J, I);

END;

END;

INC (I);

END;

l: = 0;

For i: = 1 to 5000 do

IF P [i] THEN Begin

INC (L); Pr [L]: = I;

END;

End; {getprime}

Function prime (x: longint): integer;

VAR i: integer;

Begin

PRIME: = FALSE;

For i: = 1 to L DO

IF PR [I]> = x Then Break

ELSE IF X MOD PR [I] = 0 THEN EXIT;

PRIME: = TRUE;

End; {prime}

转载请注明原文地址:https://www.9cbs.com/read-105362.html

New Post(0)