Go Down

#### yao1

##### Mar 22, 2013, 02:31 am
Write a program that does n!   (run the program 2 times using   n= 4, n=10  Use for, if, serial.print or what ever is needed.   Think about how large n can before causing issues.
N! = n(n-1)(n-2)….(1)

#### SurfCracker

#1
##### Mar 22, 2013, 03:07 am
Nope, really dont understand the question.....
sorry !

#### spatula

#2
##### Mar 22, 2013, 03:13 am
The limit is due to the fact that the maximum integer value you can have is an unsigned long, which is a bit more than 4.2E9, so Nmax is ??. Values greater than that wouldn't cause particular issues, just give wrong results. As for the algorithm, you have loop() to iterate over the allowed range.

#### Nick Gammon

#3
##### Mar 22, 2013, 03:16 amLast Edit: Mar 22, 2013, 03:19 am by Nick Gammon Reason: 1
I think I got up to 200! here:

http://www.gammon.com.au/forum/?id=11519

Code: [Select]
`2! = 23! = 64! = 245! = 1206! = 7207! = 50408! = 403209! = 36288010! = 3628800...196! = 508012211086704676250273578534744855832729752494702698292997143104359057480013603705540137242115195719262628671043031667501252088161309228461647972823682280495348903461291560889483687823263915860291345617137392657194686983749887501702176113098676677779711031060019608283576803094698692188285748113739606947612227692134400000000000000000000000000000000000000000000000197! = 100078405584080821221303894971344736599047761241456431563720437191558734323562679929991407036696693556694737848195477238497746661367777918006944650646265409257583733981874437495228286501182991424477395086576066353467353335798727837835328694280439305522603073118823862831864630209655642361092292378406702568679608855350476800000000000000000000000000000000000000000000000198! = 19815524305648002601818171204326257846611456725808373449616646563928629396065410626138298593265945324225558093942704493222553838950820027765375040827960551033001579328411138624055200727234232302046524227142061137986535960488148111891395081467526982493475408477527124840709196781511817187496273890924527108598562553359394406400000000000000000000000000000000000000000000000199! = 3943289336823952517761816069660925311475679888435866316473712666221797249817016714601521420059923119520886060694598194151288213951213185525309633124764149655567314286353816586186984944719612228107258321201270166459320656137141474266387621212037869516201606287027897843301130159520851620311758504293980894611113948118519486873600000000000000000000000000000000000000000000000200! = 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000`
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### Nick Gammon

#4
##### Mar 22, 2013, 03:22 amLast Edit: Mar 22, 2013, 03:24 am by Nick Gammon Reason: 1
Code for above:

Code: [Select]
`// BigNumber test: factorials#include "BigNumber.h"void setup ()  {  Serial.begin (115200);  while (!Serial) { }  // for Leonardo  BigNumber::begin ();  // initialize library  BigNumber fact = 1;  for (int i = 2; i <= 200; i++)    {    Serial.print(i);    Serial.print("! = ");    fact *= i;    Serial.println (fact);    }  // end of for each number  }  // end of setupvoid loop () { }`

Library: http://www.gammon.com.au/Arduino/BigNumber.zip
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### lloyddean

#5
##### Mar 22, 2013, 04:19 am

The limit is due to the fact that the maximum integer value you can have is an unsigned long, which is a bit more than 4.2E9, so Nmax is ??. Values greater than that wouldn't cause particular issues, just give wrong results. As for the algorithm, you have loop() to iterate over the allowed range.

THe compiler seems to support 'long long' and 'unsigned long long' which have the following ranges -

LLONG_MAX:    9223372036854775807LL
LLONG_MIN:   -9223372036854775807LL
ULLONG_MAX:   18446744073709551616ULL

Of course the 'print' and 'println' function are unable to print these data types but it should be easy to write something to print them.

#### yao1

#6
##### Mar 22, 2013, 04:25 am
Thank you all for your comments I think I know have the idea to work on it

#### spatula

#7
##### Mar 22, 2013, 04:32 am
Quote

I think I got up to 200! here:

Well that's impressive!

It also happens to be greater than my own estimate, but just about 370 orders of magnitude (and counting...).

#### yao1

#8
##### Mar 22, 2013, 05:14 am

Code for above:

Code: [Select]
`// BigNumber test: factorials#include "BigNumber.h"void setup ()  {  Serial.begin (115200);  while (!Serial) { }  // for Leonardo  BigNumber::begin ();  // initialize library  BigNumber fact = 1;  for (int i = 2; i <= 200; i++)    {    Serial.print(i);    Serial.print("! = ");    fact *= i;    Serial.println (fact);    }  // end of for each number  }  // end of setupvoid loop () { }`

Library: http://www.gammon.com.au/Arduino/BigNumber.zip

#### yao1

#9
##### Mar 22, 2013, 05:16 am
The code above brings up 'bignumber' not declared in the scope

#### spatula

#10
##### Mar 22, 2013, 05:43 am

The code above brings up 'bignumber' not declared in the scope

You should download the zip file and decompress it into the libraries directory of your sketchbook directory, so that you have a libraries/BigNumber directory with all the files inside. Then restart the Arduino IDE. You should find the Factorials example under File - Examples - BigNumber.

#### PeterH

#11
##### Mar 22, 2013, 05:03 pm

Write a program that does n!   (run the program 2 times using   n= 4, n=10  Use for, if, serial.print or what ever is needed.   Think about how large n can before causing issues.
N! = n(n-1)(n-2)….(1)

This is a school assignment, isn't it?
I only provide help via the forum - please do not contact me for private consultancy.

#### AWOL

#12
##### Mar 22, 2013, 05:10 pm
Quote
Write a program that does n!   (run the program 2 times using   n= 4, n=10  Use for, if, serial.print or what ever is needed.   Think about how large n can before causing issues.
N! = n(n-1)(n-2)….(1)

To iterate is human, to recurse divine.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### Nick Gammon

#13
##### Mar 22, 2013, 11:59 pm

This is a school assignment, isn't it?

As long as I get the credit. It's called "attribution" isn't it?
Please post technical questions on the forum, not by personal message. Thanks!

http://www.gammon.com.au/electronics

#### michinyon

#14
##### Mar 23, 2013, 04:01 am
The point of this apparent school assignment,  is to teach you to learn to deal with the issues of the available
size range for integers.    Make sure you use  long,  or unsigned long,   for any numbers which might go above 32000

Go Up

Please enter a valid email to subscribe