Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > your highest factorial

your highest factorial
Thread Tools
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Oct 26, 2001, 08:38 AM
 
I've got 10.1 with a G3 @ 400mhz and was playing factorials today (I was bored, don't ask!) and noticed this fun discovery:

What number:170
7.25742e+306

What number:171
Infinity

I've never seen a system show "Infinity" as a numeric response when it was larger than its container could handle.

Can a G4 go higher th an 170! ? Can you try and let me know?

Darcy
debaston@vianet.ca

ps :original source code is-

Darcy.cpp
---
#include "Darcy.h"

int main()
{
double answer;
int quest;

ask("What number:", quest);
while (quest != 0)
{
answer=factorial(quest);
cout << answer;
nl();
ask("What number:", quest);
}
return 0;
}
---
Darcy.h (not all is quoted here, but contains the function used above)

double factorial(int i)
{
double c=i;
while ((i-1)!=0)
{
c=c*(i-1);
i-=1;
}

return c;
}

void nl(void)
{
cout << "\n";
}

pss: This is old code I found from college days way back when heh.
Macbook (white glossy) 2.16GHz | 4GB RAM | 7200RPM HD | 10.5.x
     
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Oct 29, 2001, 09:43 AM
 
Well, it should give the same result, since it's dependent on the size of the double type, which is the same, 64 bits. The Altivec vector library code comes with a "bignum" library. I haven't used it, but looking at the header it looks like it'll do 1024 bit signed integers.

2^1023 = 89884656743115795386465259539451236680898848947115 328636715040578866\
33790275048156635423866120376801056005693993569667 882939488440720831\
12464237153197370621888839467124327426381511098006 230470597265414760\
42502884419075341171231440736956555270413618581675 255342293149119973\
622969239858152417678164812112068608

That's something like 10^308. The cool thing is that you get the full accuracy of 1023 bits, unlike using a floating point representation. A double probably only gets you 12-15 digits of accuracy.

dave
     
Dedicated MacNNer
Join Date: Jan 2001
Location: Boulder, CO, USA
Status: Offline
Reply With Quote
Oct 29, 2001, 11:33 AM
 
Just thought I'd mention that are schemes which will let you represent a number of arbitrary magnitude. Limited by your available memory, of course. (yikes!)
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Oct 29, 2001, 11:39 AM
 
Thanks for the info guys! I found an old floppy disk with a factorial test result on an Amiga 1200 with 68EC030 processor and it couldn't go higher than 10^37 hehe.
Macbook (white glossy) 2.16GHz | 4GB RAM | 7200RPM HD | 10.5.x
     
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status: Offline
Reply With Quote
Oct 29, 2001, 06:17 PM
 
As others have mentioned, your results are being skewed by your use of a double. Aside from the inaccuracy of floating-point representations (in the IEEE standard), there is the fact that there are three (I'm pretty sure there aren't more) number that a floar (or double) can be that are outside of the Real domain. Those values are: negative infinity, positive infinity, and NaN (not a number). You can get NaN from operations like division by zero and all operations on it yield NaN. You can have more fun with an arbitrary precision library (I've calculated 1000000!, which is a very large number indeed).
     
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Oct 29, 2001, 07:25 PM
 
Being the geek that I am, I wrote a perl script to compute 1000 factorial, using perl's BigInt module. Here ya go:


<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#! /usr/bin/perl

use Math::BigInt;

$one = Math::BigInt-&gt;new(<font color = red>"<font color = blue>1</font>"</font>);

$n = <font color = blue>1000</font>;
$test = factorial($n);

sub factorial()
{
$n = shift(@_);

my $count = Math::BigInt-&gt;new(<font color = red>"<font color = blue>2</font>"</font>);
my $result = $one;
for ($i = <font color = blue>1</font>; $i &lt; $n; $i++) {
$result = $result * $count;
print <font color = red>"$count, $result \n"</font>;
$count = $count + $one;
}
return $result;
}

</font>[/code]

dave
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Oct 29, 2001, 09:06 PM
 
Thanks Dave an everyone. You definitely know your stuff, I'm a dabbling fool basically heh. Dave, how do I run a perl script in X 10.1?
Macbook (white glossy) 2.16GHz | 4GB RAM | 7200RPM HD | 10.5.x
     
Dedicated MacNNer
Join Date: Apr 2001
Location: Bethesda, MD
Status: Offline
Reply With Quote
Oct 29, 2001, 09:37 PM
 
Originally posted by darcybaston:
<STRONG>Thanks Dave an everyone. You definitely know your stuff, I'm a dabbling fool basically heh. Dave, how do I run a perl script in X 10.1?</STRONG>
Just paste my code into a text file, then change its permission to make it executable: chmod 755 factorial.pl

The first line of the script (#! /usr/bin/perl) tells the system to run it using perl.

Or, the other way of running it is to just type: perl factorial.pl

dave
     
Grizzled Veteran
Join Date: May 2000
Location: ON, Canada
Status: Offline
Reply With Quote
Oct 29, 2001, 10:06 PM
 
Holy SH**! Look at that sucker go! That's a completely different experience Dave, thanks for sharing it! Damn, those are big numbers.

Just to humour whoever else is watching, the script stops at 1000! which is the following number:

40238726007709377354370243392300398571937486421071 46325437999104299385123986290205920442084869694048 00479988610197196058631666872994808558901323829669 944590997424504087073759918823 62772718873251977950595099527612087497546249704360 14182780946464962910563938874378864873371191810458 25783647849977012476632889835955735432513185323958 463075557409114262417474349347 55342864657661166779739666882029120737914385371958 82498081268678383745597317461360853795345242215865 93201928090878297308431392844403281231558611036976 801357304216168747609675871348 31202547858932076716913244842623613141250878020800 02616831510273418279777047846358681701643650241536 91398281264810213092761244896359928705114964975419 909342221566832572080821333186 11681155361583654698404670897560290095053761647584 77284218896796462449451607653534081989013854424879 84959953319101723355556602139450399736280750137837 615307127761926849034352625200 01588853514733161170210396817592151090778801939317 81141945452572238655414610628921879602238389714760 88506276862967146674697562911234082439208160153780 889893964518263243671616762179 16890977991190375403127462228998800519544441428201 21873617459926429565817466283029555702990243241531 81617210465832036786906117260158783520751516284225 540265170483304226143974286933 06169089796848259012545832716822645806652676995865 26822728070757813918581788896522081643483448259932 66043367660176999612831860788386150279465955131156 552036093988180612138558600301 43569452722420634463179746059468257310379008402443 24384656572450144028218852524709351906209290231364 93273497565513958720559654228749774011413346962715 422845862377387538230483865688 97646192738381490014076731044664025989949022222176 59043399018860185665264850617997023561938970178600 40811889729918311021171229845901641921068884387121 855646124960798722908519296819 37238864261483965738229112312502418664935314397013 74285319266498753372189406942814341185201580141233 44828015051399694290153483077644569099073152433278 288269864602789864321139083506 21709500259738986355427719674282224875758676575234 42202075736305694988250879689281627538488633969099 59826280956121450994871701244516461260379029309120 889086942028510640182154399457 15680594187274899809425474217358240106367740459574 17851608292301353580818400969963725242305608559037 00624271243416909004153690105933983835777939410970 027753472000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000000000000000000000000 000000000000000000000000000000000000000000000000

[ 10-29-2001: Message edited by: darcybaston ]
Macbook (white glossy) 2.16GHz | 4GB RAM | 7200RPM HD | 10.5.x
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:53 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2