Page 1 of 1 1
Topic Options
#168473 - 2006-09-26 04:55 AM Factorial via Recursion
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
This is a golfed-down version of the Fac() - Calculate Factorial of a natural number UDF.
The function basically multiplies the number n with the factorial of the previous number n-1 with the boundary condition of 1!=1
Code:

function fac($)
if $>1
$fac=cdbl($)*fac($-1)
else
$fac=1
endif
endfunction

_________________________
There are two types of vessels, submarines and targets.

Top
#168474 - 2006-09-26 05:21 AM Re: Factorial via Recursion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
nice to see you are able to golf too
Top
#168475 - 2006-09-26 05:54 AM Re: Factorial via Recursion
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Yeah, bit rusty, though. Way too much other work to do, so KiXtart is on the short end. But I'll continue hosting the KiXgolfs and will incorporate the updated rules and scoring UDF.
_________________________
There are two types of vessels, submarines and targets.

Top
#168476 - 2006-09-26 06:58 AM Re: Factorial via Recursion
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
What is fac(-3)?
[Edit]
Oh, you were golfing it. No need to check for errors.
[/Edit]


Edited by Witto (2006-09-26 08:27 AM)

Top
#168477 - 2006-09-26 09:35 AM Re: Factorial via Recursion
Richard H. Administrator Offline
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 Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
hey, no such thing as a negative factorial

cj

Top
#168479 - 2006-09-27 06:13 AM Re: Factorial via Recursion
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
No, only natural numbers (0;1;2;3;4...). No integer (...-3;-2;-1;0;1;2;3...)
-(n!) is OK
(-i!) is no go
BTW, Richard, I noticed your function that handles negative integers loops when input is negative...

Top
#168480 - 2006-09-27 07:26 AM Re: Factorial via Recursion
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
so, richard finally found the way to produce negative fractionals?
is that worth a nobel or what?

Top
#168481 - 2006-09-27 09:07 AM Re: Factorial via Recursion
Richard H. Administrator Offline
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 Offline
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 Offline
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 Offline
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 Offline
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 Offline
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
#168487 - 2006-09-28 04:48 AM Re: Factorial via Recursion
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
By default, a factorial is only defined for positive intergers. However, one can expand the factorials to include fractions and e.g. negative numbers as well. for mroe info see http://en.wikipedia.org/wiki/Factorial (light) and http://mathworld.wolfram.com/Factorial.html (heavy).
_________________________
There are two types of vessels, submarines and targets.

Top
#168488 - 2006-09-28 09:02 AM Re: Factorial via Recursion
cj Offline
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 Offline
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 Offline
MM club member
*****

Registered: 2000-04-06
Posts: 1102
Loc: Brisbane, Australia
rofl, indeed and now my brother-in-law is one

cj

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 675 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.071 seconds in which 0.024 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org