How to resolve the algorithm Proper divisors step by step in the PureBasic programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Proper divisors step by step in the PureBasic programming language
Table of Contents
Problem Statement
The proper divisors of a positive integer N are those numbers, other than N itself, that divide N without remainder. For N > 1 they will always include 1, but for N == 1 there are no proper divisors.
The proper divisors of 6 are 1, 2, and 3. The proper divisors of 100 are 1, 2, 4, 5, 10, 20, 25, and 50.
Show all output here.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Proper divisors step by step in the PureBasic programming language
Source code in the purebasic programming language
EnableExplicit
Procedure ListProperDivisors(Number, List Lst())
If Number < 2 : ProcedureReturn : EndIf
Protected i
For i = 1 To Number / 2
If Number % i = 0
AddElement(Lst())
Lst() = i
EndIf
Next
EndProcedure
Procedure.i CountProperDivisors(Number)
If Number < 2 : ProcedureReturn 0 : EndIf
Protected i, count = 0
For i = 1 To Number / 2
If Number % i = 0
count + 1
EndIf
Next
ProcedureReturn count
EndProcedure
Define n, count, most = 1, maxCount = 0
If OpenConsole()
PrintN("The proper divisors of the following numbers are : ")
PrintN("")
NewList lst()
For n = 1 To 10
ListProperDivisors(n, lst())
Print(RSet(Str(n), 3) + " -> ")
If ListSize(lst()) = 0
Print("(None)")
Else
ForEach lst()
Print(Str(lst()) + " ")
Next
EndIf
ClearList(lst())
PrintN("")
Next
For n = 2 To 20000
count = CountProperDivisors(n)
If count > maxCount
maxCount = count
most = n
EndIf
Next
PrintN("")
PrintN(Str(most) + " has the most proper divisors, namely " + Str(maxCount))
PrintN("")
PrintN("Press any key to close the console")
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
EndIf
You may also check:How to resolve the algorithm Peaceful chess queen armies step by step in the C# programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Scheme programming language
You may also check:How to resolve the algorithm Ordered words step by step in the Ruby programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Ada programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Picat programming language