Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
55
Task/Smith-numbers/FreeBASIC/smith-numbers.basic
Normal file
55
Task/Smith-numbers/FreeBASIC/smith-numbers.basic
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
|
||||
If n < 2 Then Return
|
||||
Dim factor As UInteger = 2
|
||||
Do
|
||||
If n Mod factor = 0 Then
|
||||
Redim Preserve factors(0 To UBound(factors) + 1)
|
||||
factors(UBound(factors)) = factor
|
||||
n \= factor
|
||||
If n = 1 Then Return
|
||||
Else
|
||||
' non-prime factors will always give a remainder > 0 as their own factors have already been removed
|
||||
' so it's not worth checking that the next potential factor is prime
|
||||
factor += 1
|
||||
End If
|
||||
Loop
|
||||
End Sub
|
||||
|
||||
Function sumDigits(n As UInteger) As UInteger
|
||||
If n < 10 Then Return n
|
||||
Dim sum As UInteger = 0
|
||||
While n > 0
|
||||
sum += n Mod 10
|
||||
n \= 10
|
||||
Wend
|
||||
Return sum
|
||||
End Function
|
||||
|
||||
Function isSmith(n As UInteger) As Boolean
|
||||
If n < 2 Then Return False
|
||||
Dim factors() As UInteger
|
||||
getPrimeFactors factors(), n
|
||||
If UBound(factors) = 0 Then Return False '' n must be prime if there's only one factor
|
||||
Dim primeSum As UInteger = 0
|
||||
For i As UInteger = 0 To UBound(factors)
|
||||
primeSum += sumDigits(factors(i))
|
||||
Next
|
||||
Return sumDigits(n) = primeSum
|
||||
End Function
|
||||
|
||||
Print "The Smith numbers below 10000 are : "
|
||||
Print
|
||||
Dim count As UInteger = 0
|
||||
For i As UInteger = 2 To 9999
|
||||
If isSmith(i) Then
|
||||
Print Using "#####"; i;
|
||||
count += 1
|
||||
End If
|
||||
Next
|
||||
Print : Print
|
||||
Print count; " numbers found"
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue