How to code this?
A number is cool if the sum of its digits is divisible by 5.
Given an integer n, return the nth cool number.
I already wrote a function that returns the sum of an integer's digits.
A number is cool if the sum of its digits is divisible by 5.
Given an integer n, return the nth cool number.
I already wrote a function that returns the sum of an integer's digits.
Anonymous
I did this in VBA and Excel. I choose to do the arithmetic myself by putting the digits into an array. I bump the first digit and figure out if I need to carry, and go through a range of digits. I did it that way so I could add up the digits without having to parse the number by dividing by 10 and taking remainders. That's boring. Also, since all I do is add and subtract, without any multiplication or division, might this go faster? Just a wild guess that division is harder. This all runs in an instant anyway.
This method also lets me find cool numbers in any base ("B"), just by changing a constant.
I output the digits to the spreadsheet separated by spaces so I could do this in bases > 10 without converting to letters.
The code is
Dim digits(1 To 4) As Byte
Const B = 10
Sub cool()
digits(1) = 0
digits(2) = 0
digits(3) = 0
digits(4) = 0
N = Range("N")
For i = 1 To 999
bump
If (digits(1) + digits(2) + digits(3) + digits(4)) Mod 5 = 0 Then
ct = ct + 1
Range("a" & ct) = ct
Range("b" & ct) = digits(4) & " " & digits(3) & " " & digits(2) & " " & digits(1)
If ct = N Then
Range("D1") = digits(4) & " " & digits(3) & " " & digits(2) & " " & digits(1)
End If
End If
Next i
End Sub
Sub bump()
digits(1) = digits(1) + 1
If digits(1) >= B Then
digits(1) = digits(1) - B
digits(2) = digits(2) + 1
If digits(2) >= B Then
digits(2) = digits(2) - B
digits(3) = digits(3) + 1
If digits(3) = B Then
digits(3) = digits(3) - B
digits(4) = digits(4) + 1
End If
End If
End If
End Sub
If you don't want the list of cool numbers, you can suppress that output.
The output for base 10 through the 12th cool number is
1 0 0 0 5 12 0 0 6 4
2 0 0 1 4
3 0 0 1 9
4 0 0 2 3
5 0 0 2 8
6 0 0 3 2
7 0 0 3 7
8 0 0 4 1
9 0 0 4 6
10 0 0 5 0
11 0 0 5 5
12 0 0 6 4
The output through the 12th cool hex number is
1 0 0 0 5 12 0 0 3 12
2 0 0 0 10
3 0 0 0 15
4 0 0 1 4
5 0 0 1 9
6 0 0 1 14
7 0 0 2 3
8 0 0 2 8
9 0 0 2 13
10 0 0 3 2
11 0 0 3 7
12 0 0 3 12
Hope you like this.
D g
What language are you using .
You need a function that returns true if divisible by 5
Basically modulus does this.
10 mod 5 is 0
Mod returns remainder if remainder is 0 then that is what we need.
So you have sumdigits function
And now divisible by 5
Now just start from 0 or 1 and find each cool number til count is n
Num = 0
While (count < n)
Num = num + 1
If (Mod Sumdigits(num) ==0) then count = count+1
Do
This simply repeats until the nth cool digit is found
Num holds the last or nth cool number
Its important to watch when you increment num because you want the correct value when you leave the routine.
If you want 0 as the first number then start the num = -1
Because it gets incremented first in the loop