Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

How to find the 100001 prime number in Pascal

1 Answer

0 votes
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
  
*)

 



answered Jul 16, 2025 by avibootz
...