C
Chad
The following program is my attempt to calculate the formula at the
following url...
http://i23.photobucket.com/albums/b363/CompressorX/1.jpg
#include <stdio.h>
#include <stdlib.h>
unsigned long fact(unsigned long);
unsigned long sum_fact(unsigned long);
float harmonic(unsigned long);
unsigned long fact(unsigned long n)
{
unsigned long acc = 1;
unsigned long i;
for (i = 1; i <= n; i++)
acc *= i;
return acc;
}
unsigned long sum_fact(unsigned long n)
{
unsigned long acc = 0;
unsigned long i;
for (i = 1; i <= n; i++) {
acc += fact(i);
}
return acc;
}
float harmonic(unsigned long n)
{
float acc = 0;
float i;
for (i = 1; i <= n; i++) {
acc += ((100) *1/i);
}
return acc;
}
int main(int argc, char **argv)
{
unsigned long s;
if (argc != 2) {
fprintf(stderr, "Invalid number of args\n");
return 1;
}
s = atoi(argv[1]);
printf("The final value is: %f\n", sum_fact(s)/(harmonic(s)/100));
return 0;
}
The problem is that the code seems to break around n=6 or n=7. How
would I go about calculating stuff up to say the number 100.
following url...
http://i23.photobucket.com/albums/b363/CompressorX/1.jpg
#include <stdio.h>
#include <stdlib.h>
unsigned long fact(unsigned long);
unsigned long sum_fact(unsigned long);
float harmonic(unsigned long);
unsigned long fact(unsigned long n)
{
unsigned long acc = 1;
unsigned long i;
for (i = 1; i <= n; i++)
acc *= i;
return acc;
}
unsigned long sum_fact(unsigned long n)
{
unsigned long acc = 0;
unsigned long i;
for (i = 1; i <= n; i++) {
acc += fact(i);
}
return acc;
}
float harmonic(unsigned long n)
{
float acc = 0;
float i;
for (i = 1; i <= n; i++) {
acc += ((100) *1/i);
}
return acc;
}
int main(int argc, char **argv)
{
unsigned long s;
if (argc != 2) {
fprintf(stderr, "Invalid number of args\n");
return 1;
}
s = atoi(argv[1]);
printf("The final value is: %f\n", sum_fact(s)/(harmonic(s)/100));
return 0;
}
The problem is that the code seems to break around n=6 or n=7. How
would I go about calculating stuff up to say the number 100.