Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: March 21, 2013, 08:31:38 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)
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #1 on: March 21, 2013, 09:07:08 pm » |
Nope, really dont understand the question..... sorry !
|
|
|
|
|
Logged
|
|
|
|
|
Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
|
 |
« Reply #2 on: March 21, 2013, 09:13:37 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #3 on: March 21, 2013, 09:16:44 pm » |
I think I got up to 200! here: http://www.gammon.com.au/forum/?id=115192! = 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
|
|
|
|
« Last Edit: March 21, 2013, 09:19:03 pm by Nick Gammon »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #4 on: March 21, 2013, 09:22:27 pm » |
Code for above: // 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
|
|
|
|
« Last Edit: March 21, 2013, 09:24:48 pm by Nick Gammon »
|
Logged
|
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 25
Posts: 748
|
 |
« Reply #5 on: March 21, 2013, 10:19:17 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #6 on: March 21, 2013, 10:25:43 pm » |
Thank you all for your comments I think I know have the idea to work on it
|
|
|
|
|
Logged
|
|
|
|
|
Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
|
 |
« Reply #7 on: March 21, 2013, 10:32:26 pm » |
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...).
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #8 on: March 21, 2013, 11:14:18 pm » |
Code for above: // 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
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #9 on: March 21, 2013, 11:16:25 pm » |
The code above brings up 'bignumber' not declared in the scope
|
|
|
|
|
Logged
|
|
|
|
|
Rome, Italy
Offline
Sr. Member
Karma: 20
Posts: 442
|
 |
« Reply #10 on: March 21, 2013, 11:43:27 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6787
-
|
 |
« Reply #11 on: March 22, 2013, 11:03:52 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)
This is a school assignment, isn't it?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 144
Posts: 19379
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: March 22, 2013, 11:10:25 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) To iterate is human, to recurse divine.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 226
Posts: 14121
Lua rocks!
|
 |
« Reply #13 on: March 22, 2013, 05:59:35 pm » |
This is a school assignment, isn't it?
As long as I get the credit. It's called "attribution" isn't it?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 9
Posts: 839
|
 |
« Reply #14 on: March 22, 2013, 10:01:47 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
|