How to resolve the algorithm Largest proper divisor of n step by step in the Pascal programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Largest proper divisor of n step by step in the Pascal programming language
Table of Contents
Problem Statement
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Largest proper divisor of n step by step in the Pascal programming language
Source code in the pascal programming language
program LarPropDiv;
function LargestProperDivisor(n:NativeInt):NativeInt;
//searching upwards to save time for example 100
//2..sqrt(n) aka 1..10 instead downwards n..sqrt(n) 100..10
var
i,j: NativeInt;
Begin
i := 2;
repeat
If n Mod i = 0 then
Begin
LargestProperDivisor := n DIV i;
EXIT;
end;
inc(i);
until i*i > n;
LargestProperDivisor := 1;
end;
var
n : Uint32;
begin
for n := 1 to 100 do
Begin
write(LargestProperDivisor(n):4);
if n mod 10 = 0 then
Writeln;
end;
end.
program LPD;
(*
Displays largest proper divisor for each integer in range 1..limit.
Command line:
LPD limit items_per_line
or LPD limit // items_per_line defaults to 10
or LPD // limit defaults to 100
*)
{$mode objfpc}{$H+}
uses SysUtils;
var
limit, items_per_line, nr_items, j, p : integer;
a : array of integer;
begin
// Set up defaults
limit := 100;
items_per_line := 10;
// Overwrite defaults with command-line parameters, if present
if ParamCount > 0 then
limit := SysUtils.StrToInt( ParamStr(1));
if ParamCount > 1 then
items_per_line := SysUtils.StrToInt( ParamStr(2));
WriteLn( 'Largest proper divisors 1..', limit);
// Dynamic arrays are 0-based. To keep it simple, we ignore a[0]
// and use a[j] for the integer j, 1 <= j <= limit
SetLength( a, limit + 1);
for j := 1 to limit do a[j] := 1; // stays at 1 if j is 1 or prime
// Sieve; if j is composite then a[j] := smallest prime factor of j
p := 2; // p = next prime
while p*p < limit do begin
j := 2*p;
while j <= limit do begin
if a[j] = 1 then a[j] := p;
inc( j, p);
end;
repeat
inc(p);
until (p > limit) or (a[p] = 1);
end;
// If j is composite, divide j by its smallest prime factor
for j := 1 to limit do
if a[j] > 1 then a[j] := j div a[j];
// Write the array to the console
nr_items := 0;
for j := 1 to limit do begin
Write( a[j]:5);
inc( nr_items);
if nr_items = items_per_line then begin
WriteLn;
nr_items := 0;
end;
end;
if nr_items > 0 then WriteLn;
end.
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Tcl programming language
You may also check:How to resolve the algorithm Pan base non-primes step by step in the Java programming language
You may also check:How to resolve the algorithm Integer sequence step by step in the Maple programming language
You may also check:How to resolve the algorithm Pathological floating point problems step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm XML/XPath step by step in the Phix programming language