How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the BASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the BASIC programming language

Table of Contents

Problem Statement

These define three classifications of positive integers based on their   proper divisors. Let   P(n)   be the sum of the proper divisors of   n   where the proper divisors are all positive divisors of   n   other than   n   itself.

6   has proper divisors of   1,   2,   and   3. 1 + 2 + 3 = 6,   so   6   is classed as a perfect number.

Calculate how many of the integers   1   to   20,000   (inclusive) are in each of the three classes. Show the results here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the BASIC programming language

Source code in the basic programming language

10 DEFINT A-Z: LM=20000
20 DIM P(LM)
30 FOR I=1 TO LM: P(I)=-32767: NEXT
40 FOR I=1 TO LM/2: FOR J=I+I TO LM STEP I: P(J)=P(J)+I: NEXT: NEXT
50 FOR I=1 TO LM
60 X=I-32767
70 IF P(I)<X THEN D=D+1 ELSE IF P(I)=X THEN P=P+1 ELSE A=A+1
80 NEXT
90 PRINT "DEFICIENT:";D
100 PRINT "PERFECT:";P
110 PRINT "ABUNDANT:";A


deficient   = 0
perfect = 0
abundant = 0

for n = 1 to 20000
	sum = SumProperDivisors(n)
	begin case
		case sum < n
			deficient += 1
		case  sum = n
			perfect += 1
		else
			abundant += 1
	end case
next

print "The classification of the numbers from 1 to 20,000 is as follows :"
print
print "Deficient = "; deficient
print "Perfect   = "; perfect
print "Abundant  = "; abundant
end

function SumProperDivisors(number)
	if number < 2 then return 0
	sum = 0
	for i = 1 to number \ 2
		if number mod i = 0 then sum += i
	next i
	return sum
end function

100 cls
110 defic = 0
120 perfe = 0
130 abund = 0
140 for n = 1 to 20000
150   sump = SumProperDivisors(n)
160     if sump < n then
170     defic = defic+1
180   else
190     if sump = n then
200       perfe = perfe+1
210     else
220       if sump > n then abund = abund+1
230     endif
240   endif
250 next
260 print "The classification of the numbers from 1 to 20,000 is as follows :"
270 print
280 print "Deficient = ";defic
290 print "Perfect   = ";perfe
300 print "Abundant  = ";abund
310 end
320 function SumProperDivisors(number)
330 if number < 2 then SumProperDivisors = 0
340 sum = 0
350 for i = 1 to number/2
360   if number mod i = 0 then sum = sum+i
370 next i
380 SumProperDivisors = sum
390 end function


Public Sub Main()
  
  Dim sum As Integer, deficient As Integer, perfect As Integer, abundant As Integer
  
  For n As Integer = 1 To 20000 
    sum = SumProperDivisors(n) 
    If sum < n Then 
      deficient += 1 
    Else If sum = n Then 
      perfect += 1 
    Else 
      abundant += 1 
    Endif 
  Next 
  
  Print "The classification of the numbers from 1 to 20,000 is as follows : \n" 
  Print "Deficient = "; deficient 
  Print "Perfect   = "; perfect 
  Print "Abundant  = "; abundant
  
End

Function SumProperDivisors(number As Integer) As Integer 
  
  If number < 2 Then Return 0 
  Dim sum As Integer = 0 
  For i As Integer = 1 To number \ 2 
    If number Mod i = 0 Then sum += i 
  Next 
  Return sum 
  
End Function


function sumProperDivisors(num)
  if num > 1 then
    sum = 1
    root = sqr(num)
    for i = 2 to root
      if num mod i = 0 then
        sum = sum + i
        if (i*i) <> num then sum = sum + num / i
      end if
    next i
  end if
  sumProperDivisors = sum
end function

deficient = 0
perfect = 0
abundant = 0

print "The classification of the numbers from 1 to 20,000 is as follows :"

for n = 1 to 20000
  sump = sumProperDivisors(n)
  if sump < n then
    deficient = deficient +1
  else
    if sump = n then
      perfect = perfect +1
    else
      if sump > n then abundant = abundant +1
    end if
  end if
next n

print "Deficient = "; deficient  
print "Perfect   = "; perfect
print "Abundant  = "; abundant

LET lm = 20000
DIM s(0)
MAT REDIM s(lm)

FOR i = 1 TO lm
    LET s(i) = -32767
NEXT i
FOR i = 1 TO lm/2
    FOR j = i+i TO lm STEP i
        LET s(j) = s(j) +i
    NEXT j
NEXT i

FOR i = 1 TO lm
    LET x = i - 32767
    IF s(i) < x THEN
       LET d = d +1
    ELSE
       IF s(i) = x THEN
          LET p = p +1
       ELSE
          LET a = a +1
       END IF
    END IF
NEXT i

PRINT "The classification of the numbers from 1 to 20,000 is as follows :"
PRINT
PRINT "Deficient ="; d
PRINT "Perfect   ="; p
PRINT "Abundant  ="; a
END


  

You may also check:How to resolve the algorithm XML/Input step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Red programming language
You may also check:How to resolve the algorithm Two bullet roulette step by step in the Go programming language
You may also check:How to resolve the algorithm Bioinformatics/base count step by step in the Forth programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the AppleScript programming language