How to resolve the algorithm Arithmetic numbers step by step in the XPL0 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic numbers step by step in the XPL0 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 XPL0 programming language
Source code in the xpl0 programming language
int N, ArithCnt, CompCnt, Div, DivCnt, Sum, Quot;
[Format(4, 0);
N:= 1; ArithCnt:= 0; CompCnt:= 0;
repeat Div:= 1; DivCnt:= 0; Sum:= 0;
loop [Quot:= N/Div;
if Quot < Div then quit;
if Quot = Div and rem(0) = 0 then \N is a square
[Sum:= Sum+Quot; DivCnt:= DivCnt+1; quit];
if rem(0) = 0 then
[Sum:= Sum + Div + Quot;
DivCnt:= DivCnt+2;
];
Div:= Div+1;
];
if rem(Sum/DivCnt) = 0 then \N is arithmetic
[ArithCnt:= ArithCnt+1;
if ArithCnt <= 100 then
[RlOut(0, float(N));
if rem(ArithCnt/20) = 0 then CrLf(0);
];
if DivCnt > 2 then CompCnt:= CompCnt+1;
case ArithCnt of 1000, 10_000, 100_000, 1_000_000:
[CrLf(0);
IntOut(0, N); ChOut(0, 9\tab\);
IntOut(0, CompCnt);
]
other;
];
N:= N+1;
until ArithCnt >= 1_000_000;
]
You may also check:How to resolve the algorithm Copy a string step by step in the AppleScript programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Ursala programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Cowgol programming language
You may also check:How to resolve the algorithm Function definition step by step in the Retro programming language
You may also check:How to resolve the algorithm S-expressions step by step in the AutoHotkey programming language