RosettaCodeData/Task/Population-count/Gambas/population-count.gambas
2023-07-01 13:44:08 -04:00

28 lines
1.5 KiB
Text

Public Sub Main()
Dim sEvil, sOdious As String 'To store the output for printing Evil and Odious
Dim iCount, iEvil, iOdious As Integer 'Counters
Print "First 30 numbers ^3\t"; 'Print title
For iCount = 0 To 29 'Count 30 times
Print Len(Replace(Bin(3 ^ iCount), "0", ""));; 'Get the Bin of the number, take out the '0's and the remaining
Next 'length is the Population count e.g. 3^2=9, Bin=1001, remove '0's='11', length=2
iCount = 0 'Reset iCount
Repeat 'Repeat/Until loop
If Even(Len(Replace(Bin(iCount), "0", ""))) Then 'If (as above) the result is Even then
sEvil &= Str(icount) & " " 'Add it to sEvil
Inc iEvil 'Increase iEvil
End If
If Odd(Len(Replace(Bin(iCount), "0", ""))) Then 'If (as above) the result is Odd then
sOdious &= Str(icount) & " " 'Add it to sOdious
Inc iOdious 'Increase iOdious
End If
Inc iCount 'Increase iCount
Until iEvil = 30 And iOdious = 30 'Until both iEvil and iOdious = 30 then exit the loop
Print "\n1st 30 Evil numbers =\t" & sEvil 'Print Evil
Print "1st 30 Odious numbers =\t" & sOdious 'Print Odious
End