How to resolve the algorithm Arithmetic numbers step by step in the FutureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Arithmetic numbers step by step in the FutureBasic 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 FutureBasic programming language
Source code in the futurebasic programming language
' Rosetta Code problem: https://rosettacode.org/wiki/Arithmetic_numbers
' by Rich Love, 9/21/22
' FutureBasic 7.0.14
output file "Arithmetic numbers.app"
Dim As long N = 1, ArithCnt = 0, CompCnt = 0
Dim As long Div, DivCnt, Sum, Quot
toolbox Microseconds( UnsignedWide * microTickCount )
dim as UnsignedWide Time1, Time2
window 1, @"Arithmetic numbers",(0,0,600,200)
Print "The first 100 arithmetic numbers are:"
Microseconds( @Time1 ) //start time
for N = 1 to 2000000)
Div = 1 : DivCnt = 0 : Sum = 0
while 1
Quot = N / Div
If Quot < Div Then Exit while
If Quot = Div And (N Mod Div) = 0 'N is a square
Sum += Quot
DivCnt += 1
Exit while
End If
If (N Mod Div) = 0
Sum += Div + Quot
DivCnt += 2
End If
Div ++
wend
If (Sum Mod DivCnt) = 0 'N is arithmetic
ArithCnt ++
If ArithCnt <= 100
Print Using "####"; N;
If (ArithCnt Mod 20) = 0 Then PRINT
End If
If DivCnt > 2 Then CompCnt ++
Select Case ArithCnt
Case 1e3
PRINT
PRINT USING "The #######th arithmetic number is";ArithCnt;
PRINT USING "#####,### up to which ";N;
PRINT USING "###,### are composite. ";compcnt
Case 1e4, 1e5, 1e6
PRINT USING "The #######th arithmetic number is";ArithCnt;
PRINT USING "#####,### up to which ";N;
PRINT USING "###,### are composite. ";compcnt
End Select
if ArithCnt = 1e6 then exit next
End If
next N
Microseconds( @Time2 ) //end time
float TimeTaken
TimeTaken = (Time2.lo-Time1.lo)/1000/100/10
print
print "It took " + str$(TimeTaken) + " seconds to complete." // Approx 1.2 seconds on a M1 Mac Mini ( Macmini9,1 )
handleevents
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the LFE programming language
You may also check:How to resolve the algorithm Penney's game step by step in the D programming language
You may also check:How to resolve the algorithm String length step by step in the Nim programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the 8080 Assembly programming language
You may also check:How to resolve the algorithm Plasma effect step by step in the Processing programming language