Monday, November 12, 2007

Science Experiments

I was poking around last night on the computer and found some old Bill Nye and Mr. Wizard videos. We homeschool, so today I worked with the two older kids on magnets. We messed with a little magnet kit, made a compass with a needle and glass of water, and took apart an old harddrive which has a couple heavy magnets in it that controls the mechanical arm that moves across the platters. So my point about Nye and Mr. Wizard is that I found an old clip from Wizard's show that gave a puzzle to cut up a piece of paper with a hole big enough to walk through. They seemed to enjoy that. Back to work tomorrow after a nice 4 day weekend.

Sunday, November 11, 2007

Coding Primes and E

So I've been messing with some javascript code I found to try and calculate a few different things for fun. I turned the code into C and attempted to calculate prime numbers. It is basically a loop that counts up from 1 to whatever you want and finds all the primes between.

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

#include stdio.h

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;
}