How to resolve the algorithm Weird numbers step by step in the FreeBASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Weird numbers step by step in the FreeBASIC programming language
Table of Contents
Problem Statement
In number theory, a weird number is a natural number that is abundant but not semiperfect (and therefore not perfect either). In other words, the sum of the proper divisors of the number (divisors including 1 but not itself) is greater than the number itself (the number is abundant), but no subset of those divisors sums to the number itself (the number is not semiperfect). For example:
Find and display, here on this page, the first 25 weird numbers.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Weird numbers step by step in the FreeBASIC programming language
Source code in the freebasic programming language
Function GetFactors(n As Long,r() As Long) As Long
Redim r(0)
r(0)=1
Dim As Long count,acc
For z As Long=2 To n\2
If n Mod z=0 Then
count+=1:redim preserve r(0 to count)
r(count)=z
acc+=z
End If
Next z
Return 1+acc
End Function
sub sumcombinations(arr() As Long,n As Long,r As Long,index As Long,_data() As Long,i As Long,Byref ans As Long,ref As Long)
Dim As Long acc
If index=r Then
For j As Long=0 To r-1
acc+=_data(j)
If acc=ref Then ans=1:Return
If acc>ref then return
Next j
Return
End If
If i>=n Or ans<>0 Then Return
_data(index) = arr(i)
sumcombinations(arr(),n,r,index + 1,_data(),i+1,ans,ref)
sumcombinations(arr(),n,r,index,_data(),i+1,ans,ref)
End sub
Function IsWeird(u() As Long,num As Long) As Long
Redim As Long d()
Dim As Long ans
For r As Long=2 To Ubound(u)
Redim d(r)
ans=0
sumcombinations(u(),Ubound(u)+1,r,0,d(),0,ans,num)
If ans =1 Then Return 0
Next r
Return 1
End Function
Redim As Long u()
Dim As Long SumFactors,number=2,count
Do
number+=2
SumFactors=GetFactors(number,u())
If SumFactors>number Then
If IsWeird(u(),number) Then Print number;" ";:count+=1
End If
Loop Until count=25
Print
Print "first 25 done"
Sleep
You may also check:How to resolve the algorithm Use another language to call a function step by step in the Ada programming language
You may also check:How to resolve the algorithm Averages/Mode step by step in the Slate programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Elena programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the MATLAB programming language