Qbasicnews.com

QbasicNews.Com => Challenges => Topic started by: oracle on April 25, 2003, 07:41:38 PM



Title: Statistical Challenge
Post by: oracle on April 25, 2003, 07:41:38 PM
Right, this should probably be on my site when it's up, but here goes: Create a function that calculates factorials. It should give the correct result for 0! and an appropriate result (null or 0 perhaps) for a non natural number. It should use the least amount of code as possible.


Title: Statistical Challenge
Post by: Agamemnus on April 26, 2003, 11:21:58 PM
Here's something to start us off.

Code:

FUNCTION factorial# (n#)
IF n# < 3 THEN factorial# = n# ELSE factorial# = n# * factorial#(n# - 1)
END FUNCTION


(edit)

Here's a recursive one. (note that starting from 1 and going up to N is less efficient because it multiplies by 1 and by 2!!

Code:

FUNCTION factorial2# (n#)
I# = n#: temp# = 1
DO
IF I# < 3 THEN EXIT DO
temp# = temp# * I#
I# = I# - 1: LOOP
factorial2# = temp# * I#
END FUNCTION


Now a for loop. But it still has an IF, except it is built in AFAIK.

Code:

FUNCTION factorial3# (N#)
temp# = 1
FOR I# = N# TO 3 STEP -1
temp# = temp# * I#
NEXT I#
factorial3# = temp# * I#
END FUNCTION


Title: Statistical Challenge
Post by: oracle on April 27, 2003, 03:53:45 AM
Thank goodness someone replied...  :)

Right, all three of your functions fail the challenge instantly, because I don't think you read the instructions correctly. They all give incorrect values for non-natural numbers, and for zero. But at least they are a start.

I think the function should use the long integer type rather than the double type too. Any problems with that?

I am not going to put a due date on this yet, because there is not much interest, but if interest picks up start coding quick!


Title: Statistical Challenge
Post by: Agamemnus on April 27, 2003, 09:12:56 AM
When I try to do factorial for anything on my calculator less than 1, I get "invalid input function"

0 is a good result to be "invalid input function" I think.................................

EDIT: and a double can be bigger than a long int, too!

EDIT:
Oh and besides, non-natural numbers would make it about 4 times as long.


Title: Statistical Challenge
Post by: na_th_an on April 27, 2003, 12:00:26 PM
0! = 1


Title: Statistical Challenge
Post by: oracle on April 28, 2003, 12:52:42 AM
Thank you na_th_an, someone actually knows the special case.

Quote from: "Agamemnus"
When I try to do factorial for anything on my calculator less than 1, I get "invalid input function"


Do some research on the properties of factorials. To make this challenge serious, I don't want anyone else to submit any information about the properties of factorials. If you want to compete, find out yourself and write the appropriate code. Telling everyone here what the properties of factorials are would be a little silly if you want to win, wouldn't it?

The only hint you have is that I am talking about mathematical factorials (not some other unheard of factorial), we are working in base 10 and that, as na_th_an said, 0! = 1.


Title: Statistical Challenge
Post by: Agamemnus on April 28, 2003, 09:31:07 AM
Hmph! Well my function works great for whole real numbers in the set
{1...infinity}

:|


Title: Statistical Challenge
Post by: oracle on April 29, 2003, 01:40:19 AM
Like I say, check out the properties of factorials. You can easily win this, you just need to add code for a couple of special cases.


Title: Statistical Challenge
Post by: relsoft on April 29, 2003, 05:37:23 AM
And why do you need it? :*)


Title: Statistical Challenge
Post by: Neo on April 29, 2003, 09:05:06 AM
Check out BIGINT (see in project section).

BIGINT was able to calculate everything up to and including 3000! on my computer.

That's quite a number! :P Lol

And moreover, it did it in just 230 seconds!!!


Title: Statistical Challenge
Post by: oracle on April 29, 2003, 10:16:20 PM
It's just a challenge relsoft. Though perhaps later we could all make heaps of other maths functions and make them into a lib...

edit: Projects section? And if it is so good, submit it, you may win!


Title: Statistical Challenge
Post by: Neo on May 01, 2003, 10:43:24 AM
Here are some test cases I ran oracle:

Code:
-----------------------------------------------------------------------------
TEST CASE 1:
IN THIS TEST CASE, I HAVE MY COMPUTER CALCULATE THE FIBONACCI NUMBER
10000 ( Fib(10000) )
THE TIME THE COMPUTER TOOK WILL BE PRINTED AFTER Fib(10000)
THE PURPOSE OF THIS TEST IS TO TEST THE SPEED OF SI.Add$
-----------------------------------------------------------------------------
RESULT:

Fibonacci number 10000 = 2079360823713349807211264898864283682508703609401590
31196829458665285014234556866489274560343052265155917573432971901580106247942
67250973176133810179902738038231789748346235556483191431591924532394420028067
81032040872441469346284906266838708330804825092065449334087873322637758084744
63248737976037347946482581138586315504040810172603812029199438923709428526016
47398213554479081823593715429566945149312993664846779090437799284773675379284
27066017513466483326637769864201210689135579114187277693408080350495679409464
82928805660563647181876626689707585373833526774208355741559456585420036347653
24541006121012446785689171494803262408602693091211601973938229446636049901531
96328615969907788042772028923553932967187718291564341907918652511867885682160
08975201710704994376570673424008710839088118009762597274318205395542568694608
15355918458253398234382360435762759823179896116748424269545924633204614137992
85081435201873848092358155398899089715146940613169561449778372074346137375621
86851068568260906963398154909212537145372418669116042505973537478237332681781
82198509240226955826416016690084749816072843582488613184829905383150180047844
35375155420157383310552198099812383325326122868982405177784658846107979080782
83671323847984517940110765690575221586803789615321608583872238829743804839319
29541222100800313580688585002598879566463221427820448492565073106595808837401
64899642356338610978204563412246787292184560640917436063561821688381256232166
44428229525375774927153653211342045306867424354545051032697681443701184949063
90254934942358904031509877369722437053383165360388595116980245927935225901537
63492565487238087718300830107456944400242643641475690509453507280476468449210
56800247399144905559043913692186963870929181892461571034503870502293006032416
11410707453960080170928277951834763216705242485820801423866526633816082921442
88309546325908047181932920171014782802522138565634020748979631766327887220760
77910344317001127535588134788887275038253890668230986833556957181378678829821
11710796422706778536913192342733364556727928018953989153106047379741280794091
639429908796650294603536651238230626
-----------------------------------------------------------------------------
Time taken to calculate Fib(10000): 0 seconds
Time taken to calculate Fib(1) up to Fib(10000): 55.25781 seconds


-----------------------------------------------------------------------------
TEST CASE 2:
IN THIS TEST CASE, I HAVE MY COMPUTER CALCULATE 2000!
THAT'S EQUAL TO 2000 x 1999 x 1998 x 1997 x ... x 5 x 4 x 3 x 2 x 1
THE TIME THE COMPUTER TOOK WILL BE PRINTED AFTER THE ANSWER TO 2000!
THE PURPOSE OF THIS TEST IS TO TEST THE SPEED OF SI.Multiply$.
-----------------------------------------------------------------------------
RESULT:
2000! = 331627509245063324117539338057632403828111720810578039457193543706038
07790560082240027323085973259225540235294122583410925808481741529379613138663
35263436889056340585561639406051172525718706478563935440454052439574670376741
08722970434684158343752431580877533645127487995436859247408032408946561507233
25065279765575717967153671868935905611281587160171723265715611000421401242043
38425737127001758835477968999212835289966658534055798549036573663501333865504
01172012152635488038268152152246920995206031564418565480675946497051552288205
23489999572645081406553667896953210146762267133202683155220519449446161823927
52040265297226315025747520482960647509273941658562835317795744828763145964503
73991327334177263608852490093506621610144459709412707821313732563831572302019
94991495831647094277447387032798554967429860883937632682415247883438746959582
92577405745398375015858154681362942179499723998135994810165565638760342273129
12250384709872909626622461971076605931550201895135583165357871492290916779049
70224709461193760778516511068443225590564873626653037738465039078804952460071
25494026145660722541363027549136715834060978310749452822174907813477096932415
56111339828051358600690594619965257310741177081519922564516778571458056602185
65476095237746301667942248844448579834980154803262082989096585738175188861937
66928282798884535846398965942139529844652910920091037100461494499158285880507
61867924946385180879874512891408019340074625920057098729578599643650655895612
41023101869055606030878362911050560124590899838341079936790205207685866918347
79065585447001486926569246319333376124280974200671728463619392496986284687199
93450393889367270487127172734561700354867477509102955523953547941107421913301
35681954109194146276641754216158762526285808980122244389024867718205495941575
19917012717675717874958616196659318788551418357820926014820717773317353960343
04969082070589958701381980813035590160762908388574561288217698136182483576739
21830311841471913398689284234400077924669120976673165143349443747323563657204
88444783318549416930301245316762327453678793228474738244850922831399525097325
05979127031047683601481191102229253372697693823670057565612400290576043852852
90293760647953345817966612383960526254910718666386935476610845504619810208405
06358276765265894923932495196859541716724193295306836734955440045863598381610
43059449826627530605423580755894108278880427825951089880635410567917950974017
78068878286981021901090014835206168888372025031066592206860148364983053278208
82635365580436056867812841692171330471411763121758957771226375847531235172309
90549829210134687304205898014418063875382664169897704237759406280877253702265
42653058086237930142267582118714350291863763634030017325181826207603974736959
52026426323641454468511134272021504583838510101369413130348562219166316238926
32765815355011276307825059969158824533457435437863683173730673296589355199694
45823687350883027865770087974988999234355556624068283476378468518384497364887
39524751032242221105612012958296571913681086938254757641188868793467251912461
92151144738836269591643672490071653428228152661247800463922544945170363723627
94075778454209104830546165619062217428698160297332404652020199281385488268195
10072828697010707375009276664875021747753727423515087482467202741700315811228
05896178122160747437947510950620938556674581252518376682157712807861499255876
13235295042234638787895485088576446613629039412766597804420209228133798711590
08962648789424132104549250035666706329094415793729867434214705072135889320195
80723064781498429522595589012754823971773325722910325760929790733299545056388
36264047465024508080946911607263208749414397300070411141859553027882735765481
91820024496977611113463181952827615909641897909581173386272060889104329452449
78535147014112442143055486089639578378347325323595763291438925288393986256273
24286277556314046383038916842163311344563630957196597846633855149231619633567
53551384034258041629198378222669095217701531753387302846108418865541383291719
51332117895728541662084823682817932512931237521541926970269703299477643823386
48300887153037340566638386829408848773072176226884902308493466119426018027261
38021080050782157410060548482013478595781027707077806555127725405016743323960
66253216415004808772403047611929032210154385353138685538486425570790795341176
51957118868373988068389579274374968349814292329219630977709014393684365533335
93078201813129934550242060445633405786069624719615056033948995233218004343599
67256623927196435402872055475012079854331970674797313126813523653744085662263
20676883758513278289625233328434181297762469707954343600349234315923967476363
89121152854066577836462139112474470512552263427012395270181270454916480459322
48108858674600952306793175967755581011679940005249806303763141344412269037034
98735579991600925924807505248554156826628176081544630830540667741263012444186
42041083731190931300011544705602777737243780671888997708510567272767812471988
32857695844217588895160467868204810010047816462358220838532488134270834079868
48663216272020882330872781908537884546913155602172887312190739396520926022910
14775270809308653649798585540105774502792898146036884318215086372462169678722
82169347370599286277112447690920902988320166830170273420259765671709863311216
34950217126442682711965026405422823175963087447530184719409552426341149846950
80733900800000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000
(those zeros belong there, so don't think it is some kind of bug, this answer
is exact. You try to recalculate it with your calculator!)
-----------------------------------------------------------------------------
Time taken to calculate 2000!: .2265625 seconds
Time taken to calculate 1! up to 2000!: 187.625 seconds


-----------------------------------------------------------------------------
TEST CASE 3:
IN THIS TEST CASE, I HAVE MY COMPUTER CALCULATE THE 2 ^ 16384
THAT WOULD BE EQUAL TO CALCULATING THE LARGEST UNSIGNED NUMBER + 1 THAT
FITS INTO A DATATYPE OF 2 kB = 16384 BITS.
THE TIME THE COMPUTER TOOK WILL BE PRINTED AFTER THE ANSWER TO 2^16384
THE PURPOSE OF THIS TEST IS TO TEST THE SPEED OF SI.Power$.
-----------------------------------------------------------------------------
RESULT:

2 ^ 16384 = 11897314953572317650857593266280071307634446870965102374726748212
33261358180483686904488595472612039915115437484839309258897667381308687426274
52469834156500608087163436600489752214325161953144684595234570948213584703664
74648309847847142809678456141384760443384048861229052868553132361586959998857
90106357018120815363320780964323712757164290613406875202417365323950267880089
06751737227061083564754575578079343162221345190381785963069031134385065753936
06496451932831782917676589654052851135561343697932817258880159084146752898325
38063419234888599898980623114025121674472051872439321323198402942705341366951
27473901459381689828899444517340036461792837713807441134579184857359507717043
76441917438896448853776847383222406082390790613994756753347397840164917426214
85229014847672335977897158397334226349734811441653077758250988926030894789604
67615310425726014180682302758800344195145532770159807128158959716941396560843
95049831712550622820266262000480421498082000020609934336812376238578806274797
27072877482838438705048034164633337013385405998040701908662387301605018188262
57372376627924079893171770880790174026540793097641964887786960401751769193868
79880880089442512588269696883641941339457801578443649460527136554549063271874
28531895100278695119323496808703630436193927592692344820812834297364478686862
06416904245855513653205505050818989186684686379991764754729137157350070101519
75590974530400330315206835182164941956366960777481105982849013436114692142741
21810495077979275556645164983850062051066517084647369464036640569339464837172
18335295687391204264000361161878927819571005209456276130670355184033011064510
19954351676266886696277638206043424803579064153542127329467560730069070888704
96125050068156659252761297664065498347492661798824062312210409274584565587264
84641765016012317587403472626195728908146619765155383074442470969863475362777
03562271261450525491252294480401491147956813598759685128085752442718714554540
84894986155020794806980939215658055319165641681105966454159951476908583129721
50329881658514207306148088802176981833841712939687837145957584605258314292844
72497036985481252957759209364500226514272499495807082039660828475509218911521
33321048011973883636577825533325988852156325439335021315312134081390451021255
36370790349591696312592420116787719010893525591453948821689711794326937360863
90744727927511167151271063964250813535531372135528905398026029786453197951009
76432939091924660228878912900654210118287298298707382159717184569540515403029
17330729245439178956867421964076145117360061775218699191336683703388720158207
16258682471331045133150972747134427283406066428904064966361044432177528112274
70029162858093727701049646499540220983981932786613204254226464243689610107429
92319763868154583756177353556898453605362723442427710576092486402378162966552
63149109069604880734752170051211363118704399257625086660325662137504166957199
19674223210606724721373471234021613540712188239909701971943944347480314217903
88631776777992153989217733434436890755031880083354685234437032708928414750164
05894484820012542373866800744573419109337748919596810165160691061499055724258
10895586938833067490204900368624166301968553005687040285095450484840073528643
82657040376715728651238025510995451885701347658818930000413884971588313986607
15475748164767276351164354628044011127113925291805707941934226868183532127990
68972247697191474268157912195973794192807298886952361100880264258801320928040
01192815397080113074133955000329901592497825993697435872628614398052011245436
92711140837479190078034065963213534170040688694434054721406759636409974050092
25803505672726465095506267339268892424364561897661906898424186770491035344080
39924832709791171288114017038418205860161475828420075018350032935849969186406
65905396607090695373816018876790466577596545880019371177713446983264287926228
94338016112445533539447087462049763409147542099248815521395929388007711172017
89489779370660427348098516102881545878791116097911342243355754917090544202639
72756952832073053318454199907493478105240061941972005916521478671936962543378
64981603833146354201700628817947177518115217674352016511172347727727075220056
17774821892859715834674454133710735842775791966056258388382326217896169178722
61188656327649342887724058597548777598692355306539299379011936116690074723547
46360764601872442031379944139824366828698790212922996174192728625891720057612
50934910048254596415204647792511444650073216410909934525979945569009557678868
63974870619488547490248636079218578342057937971888347796562734791123885857064
24836379072355410286787018527401653934219888361061949671961055068686961468019
03562974942408658719504100440491526647627276107051156838706340126413651723721
14099164587963476249492159045339372109375204657983001754080175388623127190423
61037129338896586028150046596078872444365564480545689033575955702988396719744
52821298414257848395400508426432773084098542002140906948541232080526852009414
67988761104145831703904739824888992280918182139342882956797173699431524604470
27290669964066816

-----------------------------------------------------------------------------
Time taken to calculate 2^16384: 290.1172 seconds



How's this? Is 2000! still to little for you? ;)

For the code, see the Projects section.

Ok, here's my submission (using my functions I wrote in the BIGINT library):
Code:
DIM Fac AS STRING
INPUT "Please enter a number:", Till%
IF Till% = 0 OR Till% = 1 THEN PRINT "1": END
IF Till% < 0 THEN END
Fac$ = "1"
CLS
PRINT "Busy..."
FOR C% = 2 TO Till%
   Fac$ = SI.Multiply$(Fac$, LTRIM$(STR$(C%)))
   LOCATE 1, 8:PRINT LTRIM$(STR$(INT(C%/Till%*100)))+"%"
NEXT C%
PRINT LTRIM$(STR$(C%))+"! = "+Fac$


Title: Statistical Challenge
Post by: Agamemnus on May 01, 2003, 11:11:34 AM
Nice, very nice.


Title: hm..
Post by: Lanzaa on May 01, 2003, 12:10:30 PM
How about adding "1 + 2 + 3 + 4 + 5+ 6......+1999 + 2000"

Which one of the ways is faster?

Code:
2000=x
x = x ^ 2 - (x ^ 2 - x) / 2



Code:
for c = 1 to 200
x = x + c
next c


Title: Statistical Challenge
Post by: Agamemnus on May 01, 2003, 01:09:20 PM
The first, as I suspected. The reason being that an exponentiation is slower than addition but not as slow as the number of additions that equals it... since exponentiations can be computed as successive approximations, AFAIK.

Code:

DIM xlim AS INTEGER, x AS LONG, c AS INTEGER: xlim = 2000: timer1 = TIMER
DIM xlimsqrt AS LONG, I AS LONG
method = 1
FOR I = 1 TO 10000
IF method = 0 THEN
x = xlim: x = x ^ 2 - (x ^ 2 - x) / 2
ELSEIF method = 1 THEN
xlimsqrt = xlim ^ 2
x = xlimsqrt - (xlimsqrt - xlim) / 2
ELSEIF method = 2 THEN
x = (xlim ^ 2 + xlim) / 2
ELSEIF method = 3 THEN
x = (xlim * xlim + xlim) / 2  ''Doesn't work!!!
ELSEIF method = 4 THEN
x = 0: FOR c = 1 TO xlim: x = x + c: NEXT c
END IF
NEXT I


Title: Statistical Challenge
Post by: toonski84 on May 01, 2003, 06:23:58 PM
always go with formulas when it's faster.  however, the formula for factorals is just *$%&*ed up.

as for summation, you try too hard:

Code:
function summation (n)
  summation = n * (n + 1) \ 2
end function


;)


Title: Statistical Challenge
Post by: oracle on May 02, 2003, 03:43:23 AM
Neo: I already said you could not use your library in another thread (I forget which one though  :wink: ). No one is to use a lib, but you veterans can pull all that hardware stuff if you want (I don't know how it will help...)

Lanzaa: WTF??? Factorials is 1*2*3*..*n for number n. What's all this addition?

So far there is 0 entries. C'mon you guys, especially agamemnus, all you have to do is account for special cases and you have a proper entry!


Title: Statistical Challenge
Post by: toonski84 on May 02, 2003, 08:39:55 AM
without using neo's lib it works the same way

Code:
function factoral& (number&)
  y& = 1
  for x& = 2 to number&
    y& = y& * x&
  next x&

  factoral& = y&
end function


but it can't go that high, only to about 2 or 3 million, the limit of a long integer.  you could always use double precision, but that would get inaccurate.  and i doubt lanzaa didnt know what a factoral was, that's why he asked a second question ("how about...")


Title: Statistical Challenge
Post by: oracle on May 02, 2003, 08:23:16 PM
That's also wrong... please read the above posts. This is easy guys, surely someone can do it correctly... or do I have to submit an entry myself and ruin it for you all?


Title: Statistical Challenge
Post by: Neo on May 03, 2003, 08:36:18 AM
OK here it is:

Code:
'My Library
'
' BIGINT
'
' A small library having support for so-called 'big integers',
' which are unsigned data types consisting of a maximum of
' 262144 bits = 32768 bytes.
' Compare this with a normal integer: 16 bits = 2 bytes!
'
' Actually, this version of BIGINT has support for:
' - String integers (max value = 32768x a 9)

' Have fun!
'
' -Neo Deus Ex Machina
'
'       X
'      XXX
'     X   X
'    XXX XXX
'
' HAR-SoftWare
'
' PS: Due to the fact that this is a pure-qb big integer
' library, I think you will get out of string space when
' using large big integers (like 10000!)

DECLARE FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
DECLARE FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS
STRING)
DECLARE FUNCTION Fac$(n AS INTEGER)
CONST FALSE = 0
CONST TRUE = NOT FALSE
CONST LN.E = 2.718281828#
DEFINT A-Z
'$DYNAMIC
'$END_META

FUNCTION SI.Add$(StrInt AS STRING, StrInt2 AS STRING)
   '
   ' This functions adds two String ints to eachother and
   ' returns the answer in a string int
   '

   'declare stuff
        DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
        DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
        len1 = LEN(StrInt)
        len2 = LEN(StrInt2)

        'pick out maximum length of the strings
        IF len1<len2 THEN
           maxlen = len2
           StrAns = SPACE$(len2 + 1)
        ELSE
           maxlen = len1
           StrAns = SPACE$(len1 + 1)
        END IF

   'calculate StrInt + StrInt2
   over% = 0
   ' PRINT maxlen
        FOR c = 1 TO maxlen
           IF c <= LEN(StrInt) THEN char1 = MID$(StrInt, len1 - c + 1, 1) ELSE char1 = "0"
      IF c <= LEN(StrInt2) THEN char2 = MID$(StrInt2, len2 - c + 1, 1) ELSE char2 = "0"
      val1 = VAL(char1)
      val2 = VAL(char2)
      ' print val1,val2
      ans$ = LTRIM$(STR$(val1 + val2 + over%))
      MID$(StrAns, LEN(StrAns) - c + 1, 1) = RIGHT$(ans$, 1)
      IF LEN(ans$) > 1 THEN
         over% = VAL(LEFT$(ans$, 1))
      ELSE
         over% = 0
      END IF
        NEXT c
        StrAns = LTRIM$(StrAns)
        IF over% > 0 THEN StrAns = LTRIM$(STR$(over%)) + StrAns
        SI.Add$ = StrAns
END FUNCTION

FUNCTION SI.Multiply$(StrInt AS STRING, StrInt2 AS STRING)
   '
   ' This functions multiplies two String ints to eachother and
   ' returns the answer in a string int
   '

   'declare stuff
        DIM len1 AS LONG, len2 AS LONG, maxlen AS LONG
        DIM StrAns AS STRING, c AS LONG, char1 AS STRING, char2 AS STRING
        DIM TempAns AS STRING
        len1 = LEN(StrInt)
        len2 = LEN(StrInt2)

        'pick out maximum length of the strings
        IF len1<len2 THEN
           maxlen = len2
           ' StrAns = SPACE$(len2 + 1)
        ELSE
           maxlen = len1
           ' StrAns = SPACE$(len1 + 1)
        END IF
        StrAns = "0"

   'calculate StrInt + StrInt2
   over% = 0
   ' PRINT maxlen
        FOR d = 1 TO len2
           IF d>1 THEN
              TempAns=STRING$(d-1,"0")
           ELSE
              TempAns=""
           END IF
           over% = 0
           char2 = MID$(StrInt2, len2 - d + 1, 1)
      val2 = VAL(char2)
           FOR c = 1 TO len1
              char1 = MID$(StrInt, len1 - c + 1, 1)
              'print char1;" ";char2
         val1 = VAL(char1)
                        ans$ = LTRIM$(STR$(val1 * val2 + over%))
                        'print ans$
         TempAns = RIGHT$(ans$, 1) + TempAns
         'print TempAns
         IF LEN(ans$) > 1 THEN
            over% = VAL(LEFT$(ans$, 1))
         ELSE
            over% = 0
         END IF
      NEXT c
      IF over% > 0 THEN TempAns = LTRIM$(STR$(over%)) + TempAns
      StrAns = SI.Add$(StrAns, TempAns)
      'PRINT StrAns
        NEXT d
        StrAns = LTRIM$(StrAns)
        SI.Multiply$ = StrAns
END FUNCTION

'---- Here is the function that can calculate factorials
FUNCTION Fac$(n AS INTEGER)
   IF n <= 1 THEN Fac$ = "1": EXIT FUNCTION
   ans$ = "1"
   FOR C% = 2 TO n
      ans$ = SI.Multiply$(ans$, LTRIM$(STR$(C%^)))
   NEXT C%
   Fac$ = ans$
END FUNCTION


Now happy? ;)


Title: Statistical Challenge
Post by: na_th_an on May 03, 2003, 09:30:16 AM
Code:
FUNCTION Factorial& (n&)
   IF n& < 0 THEN
      ERROR 18         ' Function not defined.
   ELSEIF n& = 0 THEN
      Factorial& = 1
   ELSE
      Factorial& = Factorial&(n& - 1) * n&
   END IF
END FUNCTION


Title: Statistical Challenge
Post by: Agamemnus on May 03, 2003, 10:04:47 AM
0! != 1!


Title: Statistical Challenge
Post by: na_th_an on May 03, 2003, 11:26:29 AM
Of course!!! 0! = 1, according to factorial's definition

See it by yourself:

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


Title: Statistical Challenge
Post by: whitetiger0990 on May 03, 2003, 05:24:15 PM
Code:
CLS
DEFDBL F, I
INPUT n

f = 1
 FOR I = 1 TO n
  f = I * f
 NEXT I

z$ = LEFT$(LTRIM$(STR$(n)), 1)

FOR c = 0 TO 9
c$ = LTRIM$(STR$(c))
IF z$ = c$ THEN EXIT FOR
IF c = 9 THEN PRINT "Null": END
NEXT c

PRINT f


It goes up to 18!

Is it ok?

Eidt: If you can read Scientific Notation... It goes up to 170!

Edit: I made a mistake... Try it again...


Title: Statistical Challenge
Post by: oracle on May 04, 2003, 04:09:27 AM
whitetiger: you made another mistake, but not in the code...

Na_th_an's looks good, but I'll check it when I boot into my windows partition.

Whitetiger's looks too complicated for me to decide exactly what it does but I will check that too.

Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something... keeping in mind my next mini-challenge in this thread is going to be the combination and permutation forumla...


Title: Statistical Challenge
Post by: whitetiger0990 on May 04, 2003, 10:26:31 AM
whats did i do wrong


Title: Statistical Challenge
Post by: na_th_an on May 04, 2003, 10:43:54 AM
Quote from: "oracle"
Nath: I'm wondering if instead of returning an error (and therefore stuffing up the whole prog) it should just return 0 or something


Definitely not zero. The factorial of a negative number is NOT DEFINED, so we better find a representation for "NOT A NUMBER" (NAN). As QB doesn't suport NAN natively, I inserted that error. But we could set NAN as the maximum or the minimum long integer in range, and trap the exit so if it is that value, then it is considered NAN. But that would involve making a whole math library LOL :D.

And be sure my implementation works. It is just a QB translation of the recursive definition of factorial (as in maths):

(http://mathworld.wolfram.com/fimg64.gif)

Compare that formula with my code ;)

Yo can do it iteratively also:

Code:
FUNCTION IterativeFactorial&(n&)
   IF n&<0 THEN ERROR 18
   fact&=1
   FOR i&=1 TO n&
      fact& = fact& * i&
   NEXT i&
   IterativeFactorial& = fact&
END FUNCTION


Title: Statistical Challenge
Post by: oracle on May 04, 2003, 10:09:18 PM
I'll check your new method when I get home from school. I can't really read the formula, but I'll try later, but for the moment you are de-facto winner.

whitetiger: eidt

Everyone who sees this before I get home: Take na_th_an's code and make both a permutation and a combination function. Go searching on google if you don't know the formula.

----
edit
----

I'm home. na_th_an's factorial program is the current winner, but it can be improved so the contest is not over. Hurry up, guys!

ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)


Title: Statistical Challenge
Post by: na_th_an on May 05, 2003, 07:57:49 AM
Quote from: "oracle"
ps: nath: your prog loses out because it can only do up to 12! (hint, hint!)


If you want more, just use Neo's long int library. 12! is your top in integer math at least you do some tricks.

You can use the DOUBLE data type, this way you can calculate up to 170!, but you are losing lots of ciphers and precission (QB just callculates a power so it fills with trailing zeroes in floating point).

Code:
FUNCTION fact# (n#)
   f# = 1
   FOR i# = 1 TO n#
      f# = f# * i#
   NEXT i#
   fact# = f#
END FUNCTION


I'd stick to the integer sollution. 12! is not a very big number, but it is enough for most caluclations. If you need bigger numbers, you better use neo's code.


Title: Statistical Challenge
Post by: Neo on May 06, 2003, 05:11:22 AM
;) lol


Title: Statistical Challenge
Post by: oracle on May 06, 2003, 05:37:01 AM
OK then, I'll check later on neo's code, but now: Do the permutation and combination thingy.


Title: Statistical Challenge
Post by: na_th_an on May 06, 2003, 08:35:44 AM
Permutation:

(http://mathworld.wolfram.com/p1img2256.gif)

Code:
FUNCTION permutation&(n&, k&)
   permutation& = factorial&(n&) / factorial&(n&-k&)
END FUNCTION


Combination:

(http://mathworld.wolfram.com/c2img1243.gif)

Code:
FUNCTION combination(n&, k&)
   combination& = factorial(n&) / (factorial(n&-k&) * factorial(k&))
END FUNCTION


Title: Statistical Challenge
Post by: oracle on May 06, 2003, 07:00:41 PM
Good... na_th_an 2 : others 0

Now... statistical deviation. Two functions, one sample and one population.


Title: Statistical Challenge
Post by: DrV on May 13, 2003, 06:08:18 PM
Heh... how about the binomial theorem while you're all at it... :D


Title: Statistical Challenge
Post by: oracle on May 13, 2003, 11:41:27 PM
Yeah... that's the idea. In the end I want to have enough statistical formulae to put in a lib or something, then people who make baseball/other sport games can use the stats lib to generate proper stats...

The Binomial theorem: I can't remember the formula off the top of my head, but I know it has a combination in it and na_th_an has already made that one, so it should be easy. Just do it as a function like the others.

Anyone interested in this lib idea?


Title: Statistical Challenge
Post by: DrV on May 15, 2003, 07:55:54 PM
Binomial theorem:

The (k+1)st term of (a + b) ^ n is:
Code:

factorial(n) / factorial(n - k) / factorial(k) * a ^ (n - k) * b ^ k


A library would be interesting.  Oh, and here's sums of geometric, arithmetic, and infinite geometric sequences:

G:  t1 * (1 - r ^ n) / (1 - r)    r != 1

A:  n * (t1 + tn) / 2

I:  t1 / (1 - r)

where r is the common ratio, t1 is the first term in the sequence, n is the number of terms to sum, and tn is the nth term of the sequence.


Title: Statistical Challenge
Post by: oracle on May 15, 2003, 08:00:14 PM
I'll test your Binomial when I get home. Everyone submitting code will get credited, BTW. Here are my goals for the project:

Have all the functions used in MS Excel.

Combine with BIGINT or BIGNUM to make the ultimate lib.

Feel free to submit any function you want to this lib. And the authors of BIGINT and BIGNUM may want to help me with integration of their lib with these functions. Also, perhaps the authors of BIGINT and BIGNUM may want to combine to make the best big integer lib ever.


Title: Statistical Challenge
Post by: Agamemnus on May 16, 2003, 05:27:09 PM
It won't work, because we're using different coding methods.


Title: entry
Post by: Meg on May 16, 2003, 06:29:24 PM
Code:
FUNCTION fct (n&)
     IF n& = 0 THEN
          c& = 1
     ELSEIF n& MOD 1 THEN
          c& = n& * fct(n& - 1)
     ELSE
          c& = 0
     END IF

     fct = c&
END FUNCTION


*peace

Meg.


Title: Statistical Challenge
Post by: oracle on May 17, 2003, 01:05:01 AM
Meg, your one doesn't work for me.

Agamemnus: Then don't help then.

I'm gonna ask about getting BIGINT or BIGNUM support, cos anything over 12! in QB is inaccurate without this.

Whoever wants to help, the first function is CEIL(x). This function should round up a number to the nearest integer. This should be pretty easy, or already a QB function if I haven't looked hard enough :wink:

The next function is CEILING(x, significance). This function rounds x up to the nearest multiple of significance. eg:
CEILING(1,4) returns 4
CEILING(5,4) returns 8
CEILING(81,27) returns 108

This function should return an error if the signs of each number are different.


Title: Re: entry
Post by: na_th_an on May 17, 2003, 09:13:43 AM
Quote from: "Meg"
Code:
FUNCTION fct (n&)
     IF n& = 0 THEN
          c& = 1
     ELSEIF n& MOD 1 THEN
          c& = n& * fct(n& - 1)
     ELSE
          c& = 0
     END IF

     fct = c&
END FUNCTION


*peace

Meg.


The second IF branch will be never entered. n& MOD 1 is always false, as n& MOD 1 is always 0 (every number is divisible by 1, so the remaining will be always 0).


Title: you don't understand, oracle.
Post by: Agamemnus on May 17, 2003, 10:48:56 AM
I mean, bigint and bignum can't combine.


Title: Statistical Challenge
Post by: oracle on May 18, 2003, 05:28:23 PM
Well maybe you two can get together and make a fast, accurate version of the lib. I've pm'ed Neo, but he is on holiday so we'll see.

Anyone working on the two functions above?


Title: Statistical Challenge
Post by: Agamemnus on May 18, 2003, 11:38:44 PM
Ceil is already a function. Instead of ceil, they renamed it cint, for "Conversion to Integer."
Why can't they just make it convert.to.integer(x), I don't understand.

I'm not sure what your Ceiling, does, though.


Title: Statistical Challenge
Post by: oracle on May 19, 2003, 01:07:15 AM
CINT is not quite the same... It rounds numbers from 4.5 downwards down to 4, for example. I want a function that rounds, say 4.1, 4.00000001 and 4.7 up to 5 for example. It should work for negative numbers too, eg -4.5 rounds to -4.

CEILING rounds the value x up to the nearest common multiple of "significance". So when x=4 and sig=5 then x is rounded up to the nearest c.m which is 5, but when x=6 and sig=5 x is rounded up to 10, the next multiple of 5. If x=0 then the function should return 0.

Other examples:

CEILING(22, 5) returns 25
CEILING(-1, 5) returns function error (what ever error code that is)
CEILING(3,181) returns 181


Title: Statistical Challenge
Post by: toonski84 on May 19, 2003, 01:40:51 AM
*sigh*, use youre brains!  if you want it to round up, add 1 and round down!

int(x) - round down
int(x + .5) - round
int (x + 1) - round up


Title: Statistical Challenge
Post by: oracle on May 19, 2003, 01:55:51 AM
Like I said, there may have been a function that already did that. Thanks anyway.

Code:
FUNCTION RoundUp (x)
' This function rounds a number up to the nearest whole integer.
' Supplied by toonski84

RoundUp = INT(x + 1)
END FUNCTION


ps: What should the result for RoundUp(0) be? 0 or 1?


Title: Statistical Challenge
Post by: Neo on May 19, 2003, 07:32:11 AM
What do you think???

1 of course


Title: oh, i don't see much of a use for it, though.
Post by: Agamemnus on May 19, 2003, 09:59:32 AM
Anytime I use rounding up like this CEILING of yours, I get the difference, not the next number.

Code:

sub CEILING%(n%, c%)
if n% = 0 then ceiling% = 0: exit sub
ceiling% = n% - n% mod c% + c%
end sub


Why should it return 0 if x is 0? Do you have something against zero???!????!????!????!?


Title: Statistical Challenge
Post by: oracle on May 19, 2003, 10:34:44 PM
OK, it'll return 0.

I'll test your ceiling thingy soon.


Title: ceiling
Post by: Meg on May 20, 2003, 05:22:36 AM
Code:
FUNCTION ceiling% (n%, s%)
  'this function returns the value of n% rounded UP to the nearest
  'multiple of s%

  IF SGN(n%) = -1 AND SGN(s%) = -1 THEN '*** BOTH NEGATIVE ***
    c% = (n% \ s%) * s%
  ELSEIF SGN(n%) = -1 * SGN(s%) THEN '*** 1 NEGATIVE, 1 POSITIVE ***
    'return an error here.
    'I'm not sure what the proper code for this is...
  ELSE '*** BOTH POSITIVE ***
    c% = ((n% \ s%) + 1) * s%
  END IF

  ceiling% = c%
END FUNCTION


*peace*

Meg.

p.s. not sure what that error code is, or what you want this function to do if 0 is passed as one of the values..  but the two calculations are correct, i believe.


Title: Statistical Challenge
Post by: oracle on May 20, 2003, 05:41:02 AM
This one's better. It works for decimals too (took me ages!

Code:
FUNCTION ceiling (n, c)
IF n = 0 THEN ceiling = 0: EXIT FUNCTION
IF n / c = n \ c THEN ceiling = n: EXIT FUNCTION
n$ = STR$(n)
t$ = RIGHT$(n$, 2)
t = VAL(t$)
IF t = .5 THEN n = n + .1
ce = INT(n - n MOD c + c + .5)
IF (ce - n - c) < .5 AND (ce - n - c) > 0 THEN ce = ce - c
ceiling = ce
END FUNCTION


Can someone optimise it please?


Title: Statistical Challenge
Post by: Neo on May 20, 2003, 05:45:24 AM
If I knew what you were trying to do I would 8)


Title: Statistical Challenge
Post by: oracle on May 20, 2003, 11:40:21 PM
Heh...But is there a quicker way to extract the last two characters out of a numerical string?

Also, can someone test it for special cases that make the formula invalid?


Title: Statistical Challenge
Post by: Agamemnus on May 21, 2003, 12:41:42 AM
I didn't know you were going with decimals too.

try ceiling (0,0)? or ceiling (5,-5)? or ceiling(0,1)? ceiling (1,0)?


Title: Statistical Challenge
Post by: oracle on May 21, 2003, 01:56:33 AM
ceiling(0, 0) works OK because of the first IF statement (returns 0), but the actual function in gnumeric (a spreadsheet compatible with excel) returns a #num! error. Do you think mine should too?

ceiling(5, -5) returns 5, which is in theory 5 rounded up to a multiple of -5, but I think that should return an error instead. What about anyone else?

ceiling (0,1) returns 0, which is just like the actual function, but don't you think that should return 1 instead?

ceiling(1,0) was a good one, it froze my Linux based QBasic  :wink: . I fixed this by checking if the significance was 0, and returning error code 5 if it did. (Illegal function call).

Thanks for your help, agamemnus.