How to resolve the algorithm Arithmetic numbers step by step in the VBScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Arithmetic numbers step by step in the VBScript 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:

  1. The first 100 arithmetic numbers.
  2. The xth arithmetic number where x = 1,000 and x = 10,000.
  3. 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 VBScript programming language

Source code in the vbscript programming language

'arithmetic numbers
'run with CScript

function isarit_compo(i)
     cnt=0
     sum=0
     for j=1 to sqr(i)
       if (i mod j)=0 then 
          k=i\j 
             
         if k=j then 
            cnt=cnt+1:sum=sum+j 
         else  
            cnt=cnt+2:sum=sum+j+k 
         end if
       end if
     next
   avg= sum/cnt
   isarit_compo= array((fix(avg)=avg),-(cnt>2))
end function

function rpad(a,n) rpad=right(space(n)&a,n) :end function

dim s1
sub print(s) 
  s1=s1& rpad(s,4)
  if len(s1)=40 then wscript.stdout.writeline s1:s1=""
end sub

'main program
cntr=0
cntcompo=0
i=1
wscript.stdout.writeline "the first 100 arithmetic numbers are:"
do
  a=isarit_compo(i)
  if a(0) then  
    cntcompo=cntcompo+a(1)
    cntr=cntr+1
    if cntr<=100 then print i
    if cntr=1000 then wscript.stdout.writeline vbcrlf&"1000th   : "&rpad(i,6) & " nr composites " &rpad(cntcompo,6)
    if cntr=10000 then wscript.stdout.writeline vbcrlf& "10000th  : "&rpad(i,6) & " nr composites " &rpad(cntcompo,6)
    if cntr=100000 then wscript.stdout.writeline vbcrlf &"100000th : "&rpad(i,6) & " nr composites " &rpad(cntcompo,6):exit do
  end if 
  i=i+1
loop

  

You may also check:How to resolve the algorithm Simple windowed application step by step in the Forth programming language
You may also check:How to resolve the algorithm Babbage problem step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Wren programming language
You may also check:How to resolve the algorithm Find the missing permutation step by step in the Nim programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the XLISP programming language