How to resolve the algorithm Arithmetic numbers step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic numbers step by step in the Delphi programming language
Table of Contents
Problem Statement
A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Calculate and show here:
- The first 100 arithmetic numbers.
- The xth arithmetic number where x = 1,000 and x = 10,000.
- How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Arithmetic numbers step by step in the Delphi programming language
Source code in the delphi programming language
{{works with| Delphi-6 or better}}
program ArithmeiticNumbers;
{$APPTYPE CONSOLE}
procedure ArithmeticNumbers;
var N, ArithCnt, CompCnt, DDiv: integer;
var DivCnt, Sum, Quot, Rem: integer;
begin
N:= 1; ArithCnt:= 0; CompCnt:= 0;
repeat
begin
DDiv:= 1; DivCnt:= 0; Sum:= 0;
while true do
begin
Quot:= N div DDiv;
Rem:=N mod DDiv;
if Quot < DDiv then break;
if (Quot = DDiv) and (Rem = 0) then //N is a square
begin
Sum:= Sum+Quot;
DivCnt:= DivCnt+1;
break;
end;
if Rem = 0 then
begin
Sum:= Sum + DDiv + Quot;
DivCnt:= DivCnt+2;
end;
DDiv:= DDiv+1;
end;
if (Sum mod DivCnt) = 0 then //N is arithmetic
begin
ArithCnt:= ArithCnt+1;
if ArithCnt <= 100 then
begin
Write(N:4);
if (ArithCnt mod 20) = 0 then WriteLn;
end;
if DivCnt > 2 then CompCnt:= CompCnt+1;
case ArithCnt of 1000, 10000, 100000, 1000000:
begin
Writeln;
Write(N, #9 {tab} );
Write(CompCnt);
end;
end;
end;
N:= N+1;
end
until ArithCnt >= 1000000;
WriteLn;
end;
begin
ArithmeticNumbers;
WriteLn('Hit Any Key');
ReadLn;
end.
You may also check:How to resolve the algorithm N-smooth numbers step by step in the Sidef programming language
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Bracmat programming language
You may also check:How to resolve the algorithm String prepend step by step in the Neko programming language
You may also check:How to resolve the algorithm Apply a callback to an array step by step in the R programming language