program FindPrimeNumber;
uses
Math; // For Sqrt function
// Function to check if a number is prime
function IsPrime(num: LongInt): Boolean;
var
i: LongInt;
begin
if num < 2 then
Exit(False); // Use Exit(False) to immediately return from function
// Loop from 2 up to the square root of num
// Note: Sqrt function returns a Real (float), so cast to LongInt for loop counter.
// Integer division for modulo is implicit.
for i := 2 to Round(Sqrt(num)) do
begin
if (num mod i = 0) then
Exit(False); // If divisible, it's not prime
end;
Exit(True); // If no divisors found, it's prime
end;
// Main program block
var
count: LongInt; // Counter for prime numbers
number: LongInt; // Number to check for primality
target: LongInt; // Target prime number position
begin
count := 0; // Initialize counter
number := 1; // Start checking from 1 (loop will increment to 2 first)
target := 100001; // Target prime number position
// Loop until the target number of primes is found
while count < target do
begin
Inc(number); // Increment number to check
if IsPrime(number) then
begin
Inc(count); // If prime, increment prime count
end;
end;
WriteLn('The ', target, 'st prime number is: ', number);
end.
(*
run:
The 100001st prime number is: 1299721
*)