How do I do a summation in C without math.c? - c

Hello I need to create this summation and if you put the number 30000 the response should the number of pi, however it's not working here's the summation and here's the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <ctype.h>
#include <stdbool.h>
int main( void ){
int num, k;
double pi= 0;
printf("Digite o total de termos >=30000: ");
scanf("%d", &num);
if (num < 30000){
printf("Erro.");
}else {
for (k = 1; k<= num; k++){
if (k % 2 == 0){
pi = (double)(-1)/ (2*k -1);
}else{
pi = (double)(1)/ (2*k -1);
}
pi = pi * 4;
}
printf("O valor de pi e %f", pi);
}
return 0; }

Here is a general way to sum things up:
double sum(int from, int to, double (*f)(int)) {
double ret = 0.0;
for(int i=from; i<to; i++)
ret+=f(i);
return ret;
}
And then you can write this function:
double fun(int current) {
double sign = current %2 == 0 ? -1.0 : 1.0;
return sign / (2*current - 1)
}
Finally, call it like this:
double pi = sum(1, num, fun);
Do note that this is probably not a good way to go if you're a beginner student that is looking for a solution to some homework.

#include <stdio.h>
int main(void) {
double pi=0;
for(int k=1; k<=3000; ++k)
{
pi += (2.*(k%2)-1) / (2*k-1); // This line does a summation, and uses floating point math (Not Integer Math)
}
printf("Pi : %f\n", 4*pi);
return 0;
}

Related

I'm trying to do the golden ratio with fibonacci

I'm trying to do the golden ratio with fibonacci but I get the error that the integral goes to zero
#include <stdio.h>
#include <conio.h>
#include <math.h>
int fibonacci (double n){
if (n==0 || n==1)
return n;
else
return fibonacci(n-1)+fibonacci(n-2);
}
double fibonacci_golden_ratio(int n){
double phi;
phi = fibonacci(n) / (double) fibonacci(n-1);
return phi;
}
int main(){
int i;
for (i=1;;i++){
printf ("F = %d ",fibonacci(i));
printf ("phi = %.5lf \n", fibonacci_golden_ratio(i));
if ( fabs (fibonacci_golden_ratio(i) - fibonacci_golden_ratio(i-1)) < 0.0001 )
break;
}
printf ("phi = %.5lf\n", fibonacci_golden_ratio(i));
getch();
}
how can i run the code perfectly

How to display function point in C? [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 4 months ago.
here's the code it is supposed to display the value of my function (1/1+(25xx)) in a [-1,1] interval. But when I run it I have 1 as a result!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int i,n;
double y=0;
double t=0;
double delta=0;
scanf("%d",&n);
delta=1/n;
for (i=0;i<n;i++)
{
t=t+delta;
y= 1 / (1 + (25*t*t));
printf("%lf \n",y);
}
return 0;}
Your program is working with integer math. Be sure to use double literals and to cast integers to doubles where needed.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n;
double y = 0, t = 0, delta;
scanf("%d",&n);
delta = 1 / (double)n;
for (i = 0; i < n; i++) {
t = t + delta;
y = 1.0 / (1.0 + (25.0 * t * t));
printf("%lf \n", y);
}
return 0;
}

Summation in C using Function

I was assigned to solve this problem using a function in C. I think I am going along the right lines as with the way the equation is set up now I get each individual instance of the summation, but I am stumped as to how to get the function to add the function together in the main function. Don't mind the naming convention, it is what's required for the assignment.
#include <stdio.h>
#include <math.h>
double doublef(double x,int i) {
double equation;
equation = pow(-1, i + 1) * (pow(x, i) / i);
return equation;
}
int main() {
for (int i = 1;i < 20;i++) {
double number1=doublef(0.3, i);
printf("f(0.3)=%f", number1);
}
return 0;
}
[Equation][1]
[1]: https://i.stack.imgur.com/YW8DK.png
#include <stdio.h>
#include <math.h>
double doublef(double x,int i) {
double equation;
equation = pow(-1, i + 1) * (pow(x, i) / i);
return equation;
}
int main() {
double number1=0;
for (int i = 1;i < 20;i++) {
number1+=doublef(0.3, i);
}
printf("f(0.3)=%f", number1);
return 0;
}

Calculating a Serie in C

This image is the task I should do:
Whatever I enter between -1 and 1, the outputs are always 1.0000 or 2.0000. How can I do solve this problem? Below I attached my code.
#include <stdio.h>
#include <math.h>
int main() {
int i;
float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
if ((x>-1)&&(x<1))
{
for (i=0;i<101;i++)
sum= sum + (pow(x,i));
}
printf ("result=%f",sum);
return 0;
}
if ((x>-1)&&(x<1))
With this case your code will work only if x is zero so try removing if statement and do mention what output you expect for given particular input, it will be bit more helpful to answer it.
Try this code:
#include <stdio.h>
#include <math.h>
int main() {
int i; float x;
float sum=0;
printf ("enter an x\n");
scanf ("%f",&x);
for (i=0 ;i<101; i++)
sum+= (pow(x,i));
printf ("result=%f",sum);
return 0;
}
Even upon using a type like double you haven't got enough numerical precision to sum all the powers up to 100.
Executing the following snippet, you'll notice that, while the correct (numerically speaking) result is evaluated, the loop stops way before the 100th iteration, typically at 16:
#include <stdio.h>
#include <math.h>
#include <float.h>
// Analytically calculates the limit for n -> inf of the series of powers
double sum_of_powers_limit(double x)
{
return 1.0 / (1.0 - x);
}
int main(void)
{
double x = 0.1;
const int N = 100;
double sum = 1.0;
for (int i = 1; i <= N; ++i)
{
double old_sum = sum;
sum = sum + pow(x,i);
if (old_sum == sum)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
printf(" result = %.*e\n", DBL_DECIMAL_DIG, sum);
printf("expected = %.*e\n", DBL_DECIMAL_DIG, sum_of_powers_limit(x));
return 0;
}
Also note that a more efficient way to evaluate this kind of polynomials is the Horner's method:
// Evaluates the sum s(x) = 1 + x + x^2 + ... + x^n using Horner's method
// It stops when it cannot update the value anymore
double sum_of_powers(double x, int n)
{
double result = 1.0;
for (int i = 0; i < n; ++i)
{
double old_result = result;
result = 1.0 + x * result;
if (old_result == result)
{
fprintf(stderr, "Numerical precision limit reached at i = %d\n", i);
break;
}
}
return result;
}

How to invent a code for square-root?

I want to write a code for making square-root not using pow().
here is what i have tried:
#include <stdio.h>
#include <stdlib.h>
int main(){
int a,I,sum=0,cnt=0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
}
printf("answer is:%d",cnt);
return 0;
}
for numbers like 4,9,16,... it works but for numbers like 10,17,21,.. it does not work and the result is more than it shoud be.
what is the problem?
for(I=1;;I+=2){
sum+=I;
if(sum>a)
break;
cnt++;
}
#include <stdio.h>
#include <stdlib.h>
int main(){
int a,I,sum=0,cnt=0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
}
if(sum==a)
printf("answer is:%d",cnt);
else
printf("answer is:%d",cnt-1);
return 0;
}
You can try this
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,I,cnt=0;
int sum = 0;
printf("enter number");
scanf("%d",&a);
for(I=1;sum<a;I+=2){
sum+=I;
cnt++;
if(sum > a) //add this if statement to decrement cnt by 1 when sum exceeds a.
cnt--;
}
printf("answer is:%d",cnt);
}
Input:
21
Output:
4
Use the Babylonian method:
double babyl_sqrt(double x)
{
double i;
for (i = x / 2; fabs(i * i - x) > 0.000001f; i = (i + x / i) / 2)
;
return i;
}
To get a rounded to nearest int, adjust the limit a little bit.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int sqrt_round(int a) {
int I;
int sum = 0;
int cnt = 0;
// for(I=1;sum<a;I+=2){
for (I = 1; (sum + I / 2) < a; I += 2) {
sum += I;
cnt++;
}
return cnt;
}
int main() {
int a;
printf("enter number");
scanf("%d", &a);
printf("answer is:%d\n", sqrt_round(a));
printf("answer is:%g\n", sqrt(a));
return 0;
}
#include<stdio.h>
int main()
{
float i,x=10;
int lp;
scanf("%f",&i);
for(lp=0;lp<5;lp++)
x=(x-((((x*x)-i))/(2*x)));
printf("sqaure root of %f=%f\n",i,x);
return 0;
}
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
public int mySqrt(int x) {
long start = 1;
long end = x;
while(start + 1< end) {
long mid = start + (end - start) / 2 ;
if(mid * mid == x) {
return (int)mid;
}else if(mid * mid < x) {
start = mid ;
}else {
end = mid;
}
}
if(end * end == x) {
return (int)end;
}
return (int)start;
}

Resources