#168474 - 2006-09-26 05:21 AM
Re: Factorial via Recursion
|
Lonkero
KiX Master Guru
   
Registered: 2001-06-05
Posts: 22346
Loc: OK
|
nice to see you are able to golf too
|
|
Top
|
|
|
|
#168477 - 2006-09-26 09:35 AM
Re: Factorial via Recursion
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Think I can shave a few strokes... Code:
Function fac($) $fac=Cdbl(1) While $ $fac=$fac*$ $=$-1 Loop EndFunction
If you also want negative factorials then you have to hit a couple of bogeys: Code:
Function fac($) $fac=CDbl(IIf($<0,-1,1)) $=$*IIf($,$/$,1) While $ $fac=$fac*$ $=$-1 Loop EndFunction
|
|
Top
|
|
|
|
#168478 - 2006-09-27 04:25 AM
Re: Factorial via Recursion
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
hey, no such thing as a negative factorial 
cj
|
|
Top
|
|
|
|
#168481 - 2006-09-27 09:07 AM
Re: Factorial via Recursion
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
BTW, Richard, I noticed your function that handles negative integers loops when input is negative...
Oops. Posted the wrong (over-golfed!) version. Code:
Break ON For $ = -8 to 8 ""+$+"! = "+fac($) ? Next Function fac($) $fac=CDbl(IIf($<0,-1,1)) $=$*IIf($<0,-1,1) While $ $fac=$fac*$ $=$-1 Loop EndFunction
Gives: Code:
-8! = -40320 -7! = -5040 -6! = -720 -5! = -120 -4! = -24 -3! = -6 -2! = -2 -1! = -1 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320
Quote:
hey, no such thing as a negative factorial
I'm an easy going kind of guy, and I'll provide service to all kind of numbers - real, imaginary, integer, vulgar, positive and negative. I won't stand for positive discrimination on my watch
|
|
Top
|
|
|
|
#168482 - 2006-09-27 09:29 AM
Re: Factorial via Recursion
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
lol
you know that saying -8! = -(8!) is meaningless... that is, you are assigning an arbitrary property to both sides of an equal equation. Using this logic, I can prove that 1=2:
a=1 b=1
so aČ = ab so (aČ-bČ) = (ab-bČ) as we know, (aČ-bČ) = (a+b)(a-b) and (ab-bČ) = b(a-b) so (a+b)(a-b) = b(a-b) which leaves us with (a+b) = b therefore 1=2
so we'll have none of these mathematical shenanigans on MY watch 
cj
|
|
Top
|
|
|
|
#168483 - 2006-09-27 11:48 AM
Re: Factorial via Recursion
|
Witto
MM club member
   
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Quote:
so (a+b)(a-b) = b(a-b) which leaves us with (a+b) = b
If you derive the second line from the first then you are wrong. (a+b)*(a-b) = b*(a-b) (a+b)*0 = b*0 would be more exact
|
|
Top
|
|
|
|
#168484 - 2006-09-28 02:37 AM
Re: Factorial via Recursion
|
It_took_my_meds
Hey THIS is FUN
   
Registered: 2003-05-07
Posts: 273
Loc: Sydney, Australia
|
It strikes me that negative factorials are possible, you just have to define them just as positive factorials are defined. If you choose that a negative factorial counts up to 0. Then you get..
Code:
Break ON For $ = -8 to 8 ""+$+"! = "+f($) ? Next Get $
Function f($)
$f = 1 For $i = IIf($>0,1,$) to IIf($>0,$,~) $f=CDbl($f)*$i
EndFunction
output
Code:
-8! = 40320 -7! = -5040 -6! = 720 -5! = -120 -4! = 24 -3! = -6 -2! = 2 -1! = -1 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5040 8! = 40320
Edited by It_took_my_meds (2006-09-28 03:55 AM)
|
|
Top
|
|
|
|
#168485 - 2006-09-28 02:43 AM
Re: Factorial via Recursion
|
It_took_my_meds
Hey THIS is FUN
   
Registered: 2003-05-07
Posts: 273
Loc: Sydney, Australia
|
If you only want to consider positive factorials this code is golfed down further
Code:
Function f($)
$f = 1 For $i = 1 to $ $f=CDbl($f)*$i
EndFunction
Edited by It_took_my_meds (2006-09-28 03:56 AM)
|
|
Top
|
|
|
|
#168486 - 2006-09-28 04:29 AM
Re: Factorial via Recursion
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
Quote:
If you derive the second line from the first then you are wrong. (a+b)*(a-b) = b*(a-b) (a+b)*0 = b*0 would be more exact
indeed, but the reason it works is because both sides are 0 and we don't point this out. It's a trick because we are hiding this important fact.
cj
|
|
Top
|
|
|
|
#168488 - 2006-09-28 09:02 AM
Re: Factorial via Recursion
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
hmmm, wiki says:
The factorial function is formally defined by

and you know you can't mess with Dr Math (see my other post)
in any case, I reckon you can pop a - on the front if you want 
cj
|
|
Top
|
|
|
|
#168489 - 2006-09-28 09:16 AM
Re: Factorial via Recursion
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Perhaps I should just rename the function: Code:
Function SignNeutralFactorialPreservingPositiveOrNegativeStateOfInput() ... EndFunction
and don't get me started on "0!". Bloody mathematitions, nearly as bad as physicists.
|
|
Top
|
|
|
|
#168490 - 2006-09-29 02:57 AM
Re: Factorial via Recursion
|
cj
MM club member
   
Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
|
rofl, indeed and now my brother-in-law is one 
cj
|
|
Top
|
|
|
|
Moderator: Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 1376 anonymous users online.
|
|
|