Go Down

Topic: Does anyone know how to go about this (Read 1 time) previous topic - next topic

yao1

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)

Nope, really dont understand the question.....
sorry !

spatula

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 am Last 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! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
...
196! = 508012211086704676250273578534744855832729752494702698292997143104359057480013603705540137242115195719262628671043031667501252088161309228461647972823682280495348903461291560889483687823263915860291345617137392657194686983749887501702176113098676677779711031060019608283576803094698692188285748113739606947612227692134400000000000000000000000000000000000000000000000
197! = 100078405584080821221303894971344736599047761241456431563720437191558734323562679929991407036696693556694737848195477238497746661367777918006944650646265409257583733981874437495228286501182991424477395086576066353467353335798727837835328694280439305522603073118823862831864630209655642361092292378406702568679608855350476800000000000000000000000000000000000000000000000
198! = 19815524305648002601818171204326257846611456725808373449616646563928629396065410626138298593265945324225558093942704493222553838950820027765375040827960551033001579328411138624055200727234232302046524227142061137986535960488148111891395081467526982493475408477527124840709196781511817187496273890924527108598562553359394406400000000000000000000000000000000000000000000000
199! = 3943289336823952517761816069660925311475679888435866316473712666221797249817016714601521420059923119520886060694598194151288213951213185525309633124764149655567314286353816586186984944719612228107258321201270166459320656137141474266387621212037869516201606287027897843301130159520851620311758504293980894611113948118519486873600000000000000000000000000000000000000000000000
200! = 788657867364790503552363213932185062295135977687173263294742533244359449963403342920304284011984623904177212138919638830257642790242637105061926624952829931113462857270763317237396988943922445621451664240254033291864131227428294853277524242407573903240321257405579568660226031904170324062351700858796178922222789623703897374720000000000000000000000000000000000000000000000000

Please post technical questions on the forum, not by personal message. Thanks!

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

Nick Gammon

#4
Mar 22, 2013, 03:22 am Last 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 setup
void loop () { }


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

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

lloyddean


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

Thank you all for your comments I think I know have the idea to work on it

spatula

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


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 setup
void loop () { }


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

yao1

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

spatula


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


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

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


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!

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

michinyon

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