Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default Very speciel function

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

  #2   Report Post  
Posted to microsoft.public.excel.programming
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

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Very speciel function

Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default Very speciel function

Thank's Tom
its working

Regards
alvin


"Tom Ogilvy" wrote:

Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 175
Default Very speciel function

Hi
Toppers
Thanks for your help

regards

alvin




"Toppers" wrote:

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



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Very speciel function

can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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





  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Very speciel function

You can sum the squares of the digits in a number by the following array
formula in a worksheet cell
=SUMSQ(VALUE(MID(x,ROW(OFFSET($A$1,0,0,LEN(x))),1) ))
where x is the number to be evaluated. Don't forget to array enter
(Ctrl-Shift-Enter) the formula. You can then copy the formula down enough
times to identify whether the original number was happy or not.

Your "happy" condition is unclear. Is a number determined to be according
to whether the 1st 1-digit number that it maps to is 1, or whether it
ultimately maps to 1? For instance, 7 is a 1-digit number that maps to 1 in
five additional rounds (though only 2111, 1211, 1121, 1112, 1111111 can
transition to 7 in one round).

In terms of persistent 1-digit numbers, there are only two: 1 and 4. 17 of
the 90 2-digit numbers are happy.

Jerry

"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

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Very speciel function

http://mathworld.wolfram.com/HappyNumber.html

--
Regards,
Tom Ogilvy


"Jerry W. Lewis" wrote in message
...
You can sum the squares of the digits in a number by the following array
formula in a worksheet cell
=SUMSQ(VALUE(MID(x,ROW(OFFSET($A$1,0,0,LEN(x))),1) ))
where x is the number to be evaluated. Don't forget to array enter
(Ctrl-Shift-Enter) the formula. You can then copy the formula down enough
times to identify whether the original number was happy or not.

Your "happy" condition is unclear. Is a number determined to be according
to whether the 1st 1-digit number that it maps to is 1, or whether it
ultimately maps to 1? For instance, 7 is a 1-digit number that maps to 1

in
five additional rounds (though only 2111, 1211, 1121, 1112, 1111111 can
transition to 7 in one round).

In terms of persistent 1-digit numbers, there are only two: 1 and 4. 17

of
the 90 2-digit numbers are happy.

Jerry

"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



  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Very speciel function

Demo'd from the immediate window
? ishappy(19)
True
? ishappy(20)
False

in a worksheet it would be

=ishappy(19)
or
=ishappy(B9) where B9 contains 19


from code, call it like any function that returns a boolean result.
Dim lngVal as Long
lngVal = 19
If ishappy(lngVal) then




--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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







  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Very speciel function

tom:
that's what i thought, but every function i test gives me the #name? error

i type this
=ishappy(19)
i get #name?

i've tried it on the sheet and workbook code pages.

what am i doing wrong?

--


Gary


"Tom Ogilvy" wrote in message
...
Demo'd from the immediate window
? ishappy(19)
True
? ishappy(20)
False

in a worksheet it would be

=ishappy(19)
or
=ishappy(B9) where B9 contains 19


from code, call it like any function that returns a boolean result.
Dim lngVal as Long
lngVal = 19
If ishappy(lngVal) then




--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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











  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Very speciel function

Put the code in a General module (insert|module type)

Gary Keramidas wrote:

tom:
that's what i thought, but every function i test gives me the #name? error

i type this
=ishappy(19)
i get #name?

i've tried it on the sheet and workbook code pages.

what am i doing wrong?

--

Gary

"Tom Ogilvy" wrote in message
...
Demo'd from the immediate window
? ishappy(19)
True
? ishappy(20)
False

in a worksheet it would be

=ishappy(19)
or
=ishappy(B9) where B9 contains 19


from code, call it like any function that returns a boolean result.
Dim lngVal as Long
lngVal = 19
If ishappy(lngVal) then




--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in message
...
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








--

Dave Peterson
  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,494
Default Very speciel function

thanks dave, the only place i didn't try it

--


Gary


"Dave Peterson" wrote in message
...
Put the code in a General module (insert|module type)

Gary Keramidas wrote:

tom:
that's what i thought, but every function i test gives me the #name?
error

i type this
=ishappy(19)
i get #name?

i've tried it on the sheet and workbook code pages.

what am i doing wrong?

--

Gary

"Tom Ogilvy" wrote in message
...
Demo'd from the immediate window
? ishappy(19)
True
? ishappy(20)
False

in a worksheet it would be

=ishappy(19)
or
=ishappy(B9) where B9 contains 19


from code, call it like any function that returns a boolean result.
Dim lngVal as Long
lngVal = 19
If ishappy(lngVal) then




--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in
message
...
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








--

Dave Peterson



  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 690
Default Very speciel function

It appears that non HappyNumbers have a cycle. Maybe we can use that to
speed up the code...

Function HappyNumber(n) As Boolean
Dim p As Long
Dim d As Long
Dim t As Long
Dim s As String

If n <= 0 Then
HappyNumber = False
Exit Function
End If

s = CStr(n)

For p = 1 To Len(s)
d = Mid$(s, p, 1)
t = t + d * d
Next p

Select Case t
Case 1
HappyNumber = True
Case 2, 4, 16, 37, 58, 89, 145, 42, 20
HappyNumber = False
Case Else
HappyNumber = HappyNumber(t)
End Select
End Function

?HappyNumber(19)
True

--
Dana DeLouis
Win XP & Office 2003


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
thanks dave, the only place i didn't try it

--


Gary


"Dave Peterson" wrote in message
...
Put the code in a General module (insert|module type)

Gary Keramidas wrote:

tom:
that's what i thought, but every function i test gives me the #name?
error

i type this
=ishappy(19)
i get #name?

i've tried it on the sheet and workbook code pages.

what am i doing wrong?

--

Gary

"Tom Ogilvy" wrote in message
...
Demo'd from the immediate window
? ishappy(19)
True
? ishappy(20)
False

in a worksheet it would be

=ishappy(19)
or
=ishappy(B9) where B9 contains 19


from code, call it like any function that returns a boolean result.
Dim lngVal as Long
lngVal = 19
If ishappy(lngVal) then




--
Regards,
Tom Ogilvy


"Gary Keramidas" <GKeramidasATmsn.com wrote in message
...
can i ask how this is executed?

--


Gary


"Tom Ogilvy" wrote in message
...
Function IsHappy(num As Long, _
Optional flag As Boolean = True, _
Optional cnt As Long = 1)
Static orig As Long
Dim n1 As Long
Dim str As Long
Dim tot As Long
If flag Then
orig = 0
End If
If num < 1 Then cnt = cnt + 1
tot = 0
sStr = CStr(num)
For i = 1 To Len(sStr)
n1 = CLng(Mid(sStr, i, 1))
tot = tot + n1 ^ 2
Next
If tot = 1 Then
IsHappy = True
ElseIf cnt 30 Then
IsHappy = False
Else
If flag Then orig = tot
IsHappy = IsHappy(tot, False, cnt)
End If
End Function

--
Regards,
Tom Ogilvy

"alvin Kuiper" wrote in
message
...
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








--

Dave Peterson





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel Data Validation/Lookup function does function correcty Kirkey Excel Worksheet Functions 2 May 25th 09 09:22 PM
LINKEDRANGE function - a complement to the PULL function (for getting values from a closed workbook) [email protected] Excel Worksheet Functions 0 September 5th 06 03:44 PM
Offset function with nested match function not finding host ss. MKunert Excel Worksheet Functions 1 March 21st 06 10:46 PM
Formatering af Dato m. klokkeslæt som et tal - speciel Vagn Dam Jensen Excel Worksheet Functions 0 November 16th 05 09:55 AM
User-Defined Function pre-empting Built-in Function? How to undo???? MarWun Excel Programming 1 August 6th 03 09:31 PM


All times are GMT +1. The time now is 10:30 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"