View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Toppers Toppers is offline
external usenet poster
 
Posts: 4,339
Default Very speciel function

Hi,

Try this:

Function HappyNumber(num)
Dim n() As Integer
l = Len(num)
ReDim n(l)
newnum = 0
For i = 1 To l
newnum = newnum + CInt(Mid(num, i, 1)) ^ 2
Next i
HappyNumber = newnum
End Function


Sub test()
num = 24
snum = num
Do While num 10
num = HappyNumber(num)
Loop
If num = 1 Then
MsgBox snum & " is a happy number"
Else
MsgBox snum & " is not a happy number"
End If
End Sub


HTH
"alvin Kuiper" wrote:

Hi!

I want to check if a number is a happy number
a number number is like this
(sorry i can't write it correct with in 2 hope you understand)
Number is 19

1 in 2 + 9 in 2 = 82
8 in 2 + 2 in 2 = 68
6 in 2 + 8 in 2 = 100
1 in 2 + 0in 2 + 0in 2 = 1

This is not a Happy number
Number = 24

2in2 + 4in2 = 20
2in2 + 0in2 = 4

So only when the last ciffer is one it is a happy number

I can make it by using if and if and if...........................

But isn't there a easy way to do this?
Please help i can't see it..........

Best regards

Alvin