How to resolve the algorithm Arithmetic numbers step by step in the Ring programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic numbers step by step in the Ring 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 Ring programming language
Source code in the ring programming language
// Author: Gal Zsolt - 2023.02.26.
see "works..." + nl
divisors = []
divSum = 0
limit = 20000
counta = 0
countb = 0
countCompa = 0
countCompb = 0
for n = 1 to limit
num = 0
divSum = 0
for m = 1 to n
if n%m = 0
num++
divSum = divSum + m
ok
next
for x = 1 to n
if divSum/num = x
add(divisors,n)
counta++
countb++
if counta < 1001
if not isPrime(n) and n!=1
countCompa++
ok
ok
if counta = 1000
countNuma = n
ok
if countb < 10001
if not isPrime(n) and n!=1
countCompb++
ok
ok
if countb = 10000
countNumb = n
exit 2
ok
ok
next
next
see "The first 100 arithmetic numbers are:" + nl + nl
row = 0
for n = 1 to 100
row++
see "" + divisors[n] + " "
if row%10=0
see nl
ok
next
see nl
see "1000th arithmetic number is " + countNuma + nl
see "Number of composite arithmetic numbers <= " + countNuma + ":" + countCompa + nl+nl
see "10000th arithmetic number is " + countNumb + nl
see "Number of composite arithmetic numbers <= " + countNumb + ":" + countCompb + nl
see "done..." + nl
func isPrime num
if (num <= 1) return 0 ok
if (num % 2 = 0 and num != 2) return 0 ok
for i = 3 to floor(num / 2) -1 step 2
if (num % i = 0) return 0 ok
next
return 1
You may also check:How to resolve the algorithm Abstract type step by step in the Nit programming language
You may also check:How to resolve the algorithm RPG attributes generator step by step in the APL programming language
You may also check:How to resolve the algorithm Mayan numerals step by step in the Cowgol programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the Factor programming language
You may also check:How to resolve the algorithm Nth root step by step in the Racket programming language