Pages: [1] 2   Go Down
Author Topic: Does anyone know how to go about this  (Read 859 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I got up to 200! here:

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

Code:
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

« Last Edit: March 21, 2013, 09:19:03 pm by Nick Gammon » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code for above:

Code:
// 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 Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code for above:

Code:
// 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 Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Shannon Member
****
Karma: 184
Posts: 11197
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24449
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a school assignment, isn't it?

As long as I get the credit. It's called "attribution" isn't it?
Logged

Offline Offline
Edison Member
*
Karma: 29
Posts: 2047
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: