3
#include <iostream>
#include <iomanip>
using namespace std;

int a[8], e[8];
void term (int n)
{
    a[0]=1;
    for (int i=0; i<8; i++)
    {
      if (i<7)
      {
        a[i+1]+=(a[i]%n)*100000;
      }
    /* else
      {
        a[i+1]+=((a[i]/640)%(n/640))*100000;
      }
    */
      a[i]=a[i]/(n);
    }
}

void sum ()
{

}

int factorial(int x, int result = 1) 
{
  if (x == 1) 
    return result;
  else return factorial(x - 1, x * result);
}


int main()    
{
  int n=1;
  for (int i=1; i<=30; i++)
  {
     term(n);

     cout << a[0] << " "<< a[1] << " " << a[2] <<  " " 
          << a[3] <<  " " << a[4] << " " << a[5]<< " " 
          << " " << a[6] << " " << a[7] << endl;
     n++;
     for (int j=1; j<8; j++) 
       a[j]=0;
  }
  return 0;
}    

That what I have above is the code that I have thus far. the Sum and the rest are left purposely uncompleted because that is still in the building phase.

Now, I need to make an expansion of euler' number, This is supposed to make you use series like x[n] in order to divide a result into multiple parts and use functions to calculate the results and such.

According to it, I need to find the specific part of the Maclaurin's Expansion and calculate it.

So the X in e=1+x+(1/2!)*x and so on is always 1 Giving us e=1+1+1/2!+1/3!+1/n! to calculate

The program should calculate it in order of the N

so If N is 1 it will calculate only the corresponding factorial division part; meaning that one part of the variable will hold the result of the calculation which will be x=1.00000000~ and the other will hold the actual sum up until now which is e=2.000000~

For N=2 x=1/2!, e=previous e+x

for N=3 x=1/3!, e=previous e+x

The maximum number of N is 29 each time the result is calculated, it needs to hold all the numbers after the dot into separate variables like x[1] x[2] x[3] until all the 30~35 digits of precision are filled with them. so when printing out, in the case of N=2

x[0].x[1]x[2]x[3]~ should come out as 0.50000000000000000000 where x[0] should hold the value above the dot and x[1~3] would be holding the rest in 5 digits each.

Well yeah Sorry if my explanation sucks but This is what its asking. All the arrays must be in Int and I cannot use others And I cant use bigint as it defeats the purpose

The other problem I have is, while doing the operations, it goes well till the 7th. Starting from the 8th and so on It wont continue without giving me negative numbers.

for N=8 It should be 00002480158730158730158730. Instead I get 00002 48015 -19220 -41904 30331 53015 -19220

That is obviously due to int's limit and since at that part it does 1936000000%40320 in order to get a[3]'s value which then is 35200 which is then multiplied by 100000 giving us a 3520000000/40320, though the value of a[3] exceeds the limit of integer, any way to fix this? I cannot use doubles or Bigints for this so if anyone has a workaround for this, it would be appreciated.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
t3h_lolz
  • 31
  • 3
  • 5
    Then technically you should have marked this with the homework tag. I did that for you this time. – David Hammen Jul 17 '11 at 12:39
  • As a starter, you might want to call the function `factorial`. You define it but never use it. – David Hammen Jul 17 '11 at 12:45
  • Yeah, the later part of my post which says it exceeds the limit is when I start calling the factorial. – t3h_lolz Jul 17 '11 at 22:34
  • you can declare numbers as `unsigned int`, which will force the number to be interpreted as a positive number with a larger range ... – Foo Bah Jul 19 '11 at 16:58
  • Btw, a nicer way to write `factorial` would be, `if (n == 0) return 1; return n * factorial(n-1);`. (Not to mention that yours doesn't cover `n == 0`.) – Paul Manta Nov 13 '11 at 11:22

3 Answers3

0

32bit int limits fact to 11!

  • so you have to store all the above facts divided by some number
    • 12!/10000
    • 13!/10000
    • when it does not fit anymore use 10000^2 and so on
  • when using the division result is just shifted to next four decimals ... (as i assumed was firstly intended)
  • of course you do not divide 1/n!
    • on integers that will be zero instead divide 10000
    • but that limits the n! to only 9999 so if you want more add zeroes everywhere and the result are decimals
  • also i think there can be some overflow so you should also carry on to upper digits
Spektre
  • 49,595
  • 11
  • 110
  • 380
0

You cannot use floating point or bigint, but what about other compiler intrinsic integral types like long long, unsigned long long, etc.? To make it explicit you could use <stdint.h>'s int64_t and uint64_t (or <cstdint>'s std::int64_t and std::uint64_t, though this header is not officially standard yet but is supported on many compilers).

metal
  • 6,202
  • 1
  • 34
  • 49
0

I don't know if this is of any use, but you can find the code I wrote to calculate Euler's number here: http://41j.com/blog/2011/10/program-for-calculating-e/

new299
  • 804
  • 1
  • 7
  • 17