The second program was to try and calculate 10 digit primes off e. This didn't work so well. I found that even your long datatypes have limits. There are some libraries out there that let you use bigger numbers (bmp) but I didn't feel like messing with that.
The last program was to calculate e as far as I could take it. First I tried a method I found off the web. Using the formula (1-1/n)^n I tried to write a program to use this and do as far as n was set to a variable. C didn't like using fractions and floats for me. I then changed to another method I read about which was calculating up to a certain set of digits. Using a method like so:
1+1+1/2!+1/3!...1/n!
Where n is a variable set on how deep to calculate it. Setting n lets you calculate e to n-3. As an fyi for review, 1/3! means 1/(1*2*3). So the code has a nested loop to it that helps with calculating this. I'll try and post the code in the comments for any interested.
Ok...trying to post it here since comments get angry with what it thinks is html code.
PRIMES.C
int main(){
unsigned long int maxNumber = 999;
int primeFlag = 1;
unsigned long int maxTest;
unsigned long int Test;
unsigned long int i;
for ( i = 100; i <= maxNumber; i++ ){
primeFlag = 1;
maxTest = i / 2;
if ( (i != 2) && (( i % 2 ) == 0 ) ){
primeFlag = 0;
}
Test = 3;
while ( ( Test <= maxTest) && ( primeFlag == 1 ) ) {
if (( i % Test ) == 0 ){
primeFlag = 0;
}
Test = Test + 2;
}
if ( primeFlag == 1 ){
printf ( "%ld, ", i );
}
}
printf ("\n");
}
CALC_E.CPP
#include iostream
#include iomanip
using namespace std;
int main(){
int howdeep = 18;
int i, j;
double frac, denom, my_e;
//double e = (1+1/howdeep)^howdeep; //doesn't work - can't put exponent on it
//cout << "E: " << setiosflags(ios::fixed) << setprecision(5) << e << endl;
for ( i = 2; i <= howdeep; i++ ){
denom = 1;
for ( j = 1; j <= i; j++ ){
denom = j * denom;
//cout << "denom: " << denom << endl;
}
frac = 1 / denom;
my_e = frac + my_e;
}
my_e += 2;
cout << "E: " << setprecision(15) << my_e << endl;
return 0;
}
No comments:
Post a Comment