Finding the minimum number (named as "n") from the function of e - c

good day, I am making a program that is using the e function:
e(n) = 1 + (1/1!) + (1/2!)+ ... + (1/n!)
with this function of C:
double e(double x, double x2){
double sum, kf;
int i, m, n;
// printf("Please input the number of x: ");
// scanf("%lf", &x);
sum = 0.0;
for (i=1; i<=x; i++){
kf=1.0;
for (m=1; m<=i; m++) {
kf*=1.0/m;
}
sum+=kf;
}
return printf("e=%lf\n", 1+sum);
}
well, it was the e function. now, I want to find the minimum number n that makes:
|e(n) - e(n+1)| < x
(absolute value of e(n) - e(n+1) is smaller than x
where x is a long float that users input, will be 0.1, 0.001, 0.0001,...
Any answers are highly appreciated.

Using algebra and the e function definition, |e(n) - e(n+1)| < x can be simplified to (1/n!) < x, which can be expanded to (1/1) * (1/2) * (1/3) * ... * (1/n) < x.
int minN(double x) {
double sum = 1;
int n;
for(n = 0; sum >= x; n++) {
sum *= 1/(n+1);
}
return n;
}

Related

Miscalculation of Lagrange interpolation formula for higher degree

I am approximating Runge’s function using Lagrange’s interpolation formula for 50 interpolation points. I have written the following program to do this, but I am getting the wrong value for x= -0.992008. That wrong value is 4817543.091313, but it should be 5197172.55933613. I have got this value from the following link: Link The code used are as follows:
#include <stdio.h>
#include <math.h>
double
runge(double x)
{
return (1 / (1 + (25 * x * x)));
}
double
ab(double x)
{
if (x < 0)
return -1 * x;
return x;
}
double
lag_func(double x, double *y_i, double *x_i, int n)
{
double ex = 0.0;
for (int i = 0; i <= n; i++) {
double numer = 1.0,
denom = 1.0,
prod = 1.0;
for (int j = 0; j <= n; j++) {
if (i != j) {
numer = (x - x_i[j]);
denom = (x_i[i] - x_i[j]);
prod *= numer / denom;
}
}
ex += (prod) * y_i[i];
}
return ex;
}
int
main()
{
int n;
scanf("%d", &n);
double y_i[n + 1],
x_i[n + 1];
for (int i = 0; i < n + 1; i++) {
x_i[i] = ((2 * (double) i) / (double) n) - 1;
y_i[i] = runge(x_i[i]);
}
printf("%lf\n", lag_func(-0.992008, y_i, x_i, n));
return 0;
}
The web site is rounding its Runge coefficients to six digits. Given the magnitudes of the terms involved, up to 3.9978•1011, this introduces multiple errors up to around 2•105.
This can be seen by inserting y_i[i] = round(y_i[i] * 1e6) / 1e6; after y_i[i] = runge(x_i[i]);. Then the output of the program is 5197172.558199, matching the web site’s inaccurate result.
The web site is wrong; the result of the code in the question is better.

Calculating Pi with C

I want to calculate pi but I am getting 3.058403 as a result. How can I fix this code?
#include <stdio.h>
int main(){
double x = 0;
float g =0;
printf("How many terms to calculate pi to? ");
scanf("%f", &g);
int n;
for (n = 0; n < g ; n++){
double z = 1.0 / (2 * n + 1);
if ((n % 2) == 1){
z = z * -1;
}
x = (x + z);
}
double p = 4 * x;
printf("The value of pi is: %f", p);
return 0;
}
This is not really an answer because the actual answer has been discussed in the comment section.
Corrected version of your code:
removed pointless parentheses
use meaningful variable names
use of int for integer comparison
declaration of variables as close as possible to their scope
code formatted properly
#include <stdio.h>
int main() {
printf("How many terms to calculate pi to? ");
int nbofterms;
scanf("%d", &nbofterms);
double x = 0;
for (int n = 0; n < nbofterms; n++) {
double z = 1.0 / (2 * n + 1);
if (n % 2 == 1) {
z *= -1;
}
x = (x + z);
}
double pi = 4 * x;
printf("The value of pi is: %f", pi);
return 0;
}

Gradient descent returning nan

I need to write a function to get a curve fit of a dataset. The code below is what I have. It attempts to use gradient descent to find polynomial coefficients which best fit the data.
//solves for y using the form y = a + bx + cx^2 ...
double calc_polynomial(int degree, double x, double* coeffs) {
double y = 0;
for (int i = 0; i <= degree; i++)
y += coeffs[i] * pow(x, i);
return y;
}
//find polynomial fit
//returns an array of coefficients degree + 1 long
double* poly_fit(double* x, double* y, int count, int degree, double learningRate, int iterations) {
double* coeffs = malloc(sizeof(double) * (degree + 1));
double* sums = malloc(sizeof(double) * (degree + 1));
for (int i = 0; i <= degree; i++)
coeffs[i] = 0;
for (int i = 0; i < iterations; i++) {
//reset sums each iteration
for (int j = 0; j <= degree; j++)
sums[j] = 0;
//update weights
for (int j = 0; j < count; j++) {
double error = calc_polynomial(degree, x[j], coeffs) - y[j];
//update sums
for (int k = 0; k <= degree; k++)
sums[k] += error * pow(x[j], k);
}
//subtract sums
for (int j = 0; j <= degree; j++)
coeffs[j] -= sums[j] * learningRate;
}
free(sums);
return coeffs;
}
And my testing code:
double x[] = { 0, 1, 2, 3, 4 };
double y[] = { 5, 3, 2, 3, 5 };
int size = sizeof(x) / sizeof(*x);
int degree = 1;
double* coeffs = poly_fit(x, y, size, degree, 0.01, 1000);
for (int i = 0; i <= degree; i++)
printf("%lf\n", coeffs[i]);
The code above works when degree = 1, but anything higher causes the coefficients to come back as nan.
I've also tried replacing
coeffs[j] -= sums[j] * learningRate;
with
coeffs[j] -= (1/count) * sums[j] * learningRate;
but then I get back 0s instead of nan.
Anyone know what I'm doing wrong?
I tried degree = 2, iteration = 10 and got results other than nan (values around a few thousands) Adding one to iteration seems making magnitude of the results larger by about 3 times after that.
From this observation, I guessed that the results are being multiplied by count.
In the expression
coeffs[j] -= (1/count) * sums[j] * learningRate;
Both of 1 and count are integers, so integer division is done in 1/count and it will become zero if count is larger than 1.
Instead of that, you can divide the result of multiplication by count.
coeffs[j] -= sums[j] * learningRate / count;
Another way is using 1.0 (double value) instead of 1.
coeffs[j] -= (1.0/count) * sums[j] * learningRate;
Aside:
A candidate NAN source is adding opposite signed values where one is an infinity. Given OP is using pow(x, k), which grows rapidly, using other techniques help.
Consider a chained multiplication rather than pow(). The result is usually more numerically stable. calc_polynomial() for example:
double calc_polynomial(int degree, double x, double* coeffs) {
double y = 0;
// for (int i = 0; i <= degree; i++)
for (int i = degree; i >= 0; i--)
//y += coeffs[i] * pow(x, i);
y = y*x + coeffs[i];
}
return y;
}
Similar code could be used for the main() body.

Run Time Error in finding the probability of prime distance between two nodes of tree

Intro to problem
You are given a tree. If we select 2 distinct nodes uniformly at random, what's the probability that the distance between these 2 nodes is a prime number?
Input
The first line contains a number N: the number of nodes in this tree.
The following N-1 lines contain pairs a[i] and b[i], which means there is an edge with length 1 between a[i] and b[i].
Output
Output a real number denote the probability we want.
You'll get accept if the difference between your answer and standard answer is no more than 10^-6.
#include<stdio.h>
#include<math.h>
int checkprime(int d);
int fact(int m);
void main()
{
int N, i, j, f, p = 0, t, d, x, y, z;
float result;
int a[49999], b[49999];
printf("Enter the number of nodes\n");
scanf("%d", &N);
printf("\n");
for (i = 0; i < N - 1; i++)
{
scanf("%d\t%d", &a[i], &b[i]);//Inputting the nodes
}
for (i = 0; i < N - 1; i++)
{
for (j = i; j < N - 1; j++)
{
d = b[j] - a[i];//Taking distance between nodes
f = checkprime(d);//Checking if it is prime
if (f == 1)
{
p = p + 1;//If found prime,then increasing the number of
//possibilities
}
}
}
x = fact(N);
y = fact(2);
z = fact(N - 2);
y = y * z;
t = x / y;//finding C(N,2).Combination of number of nodes and pair of 2
result = p / t;//finding probability
printf("\n\n%f", result);
}
int checkprime(int d)//function to check prime
{
int k, flag = 1;
for (k = 2; k < d / 2; k++)
{
if (d % k == 0)
{
flag = 0;
}
}
return flag;
}
int fact(int m)//function to calculate factorial
{
int k, r = 1;
for (k = m; k > 1; k--)
{
r = r * k;
}
return r;
}

consider x^i+y^i=z^i x<=y<=z<=m and 2<=i<=n (m and n are inputs) m can vary from 5 to 100 n can vary from 2 to 100

consider x^i+y^i=z^i, x<=y<=z<=m and 2<=i<=n
(m and n are inputs)
m can vary from 5 to 100
n can vary from 2 to 100
How can i aproach this problem.? I've a solution but it's not feasible for values of n and m like 80 or more and starts giving wrong results :(
int main()
{
int m, n;
long long int x, y, z, j;
long long int xe, ye, ze, se;
long long int sum = 0;
scanf("%d", &m);
scanf("%d", &n);
for (j = 2; j <= n; j++)
{
for (x = 0; x <= m; x++)
{
for (y = x; y <= m; y++)
{
for (z = y; z <= m; z++)
{
xe = pow(x, j);
ye = pow(y, j);
ze = pow(z, j);
se = (xe + ye);
if (ze == se)
{
printf("\n i = %lld", j);
sum++;
}
}
}
}
}
printf("sum= %lld ", sum);
return 0;
}
You need an implementation of a Big Integer in C, as your predicted Results can get higher than the normal Integer, even the long long int is capable of storing. You can either write one yourself or use one that has already been written, like here.

Resources