Qbasicnews.com
September 28, 2020, 12:21:43 AM
 Pages: [1] 2 3 4
 Author Topic: Statistical Challenge  (Read 20683 times)
oracle
*/-\*

Posts: 3652

 « 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.
 Logged

Agamemnus
x/ \z

Posts: 3491

 « Reply #1 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
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
oracle
*/-\*

Posts: 3652

 « Reply #2 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!
 Logged

Agamemnus
x/ \z

Posts: 3491

 « Reply #3 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.
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
na_th_an
*/-\*

Posts: 8244

 « Reply #4 on: April 27, 2003, 12:00:26 PM »

0! = 1
 Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
oracle
*/-\*

Posts: 3652

 « Reply #5 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.
 Logged

Agamemnus
x/ \z

Posts: 3491

 « Reply #6 on: April 28, 2003, 09:31:07 AM »

Hmph! Well my function works great for whole real numbers in the set
{1...infinity}

:|
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
oracle
*/-\*

Posts: 3652

 « Reply #7 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.
 Logged

relsoft
*/-\*

Posts: 3927

 « Reply #8 on: April 29, 2003, 05:37:23 AM »

And why do you need it? :*)
 Logged

y smiley is 24 bit.

Genso's Junkyard:
http://rel.betterwebber.com/
Neo
Na_th_an

Posts: 2150

 « Reply #9 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! Lol

And moreover, it did it in just 230 seconds!!!
 Logged
oracle
*/-\*

Posts: 3652

 « Reply #10 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!
 Logged

Neo
Na_th_an

Posts: 2150

 « Reply #11 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\$
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #12 on: May 01, 2003, 11:11:34 AM »

Nice, very nice.
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Lanzaa
Forum Regular

Posts: 105

 « Reply #13 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
 Logged
Agamemnus
x/ \z

Posts: 3491

 « Reply #14 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
 Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
 Pages: [1] 2 3 4