Calculation of value of Pi up to 'n' decimal places [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I was trying to make a C++ program to calculate value of Pi up to 'n' decimal places using the Leibniz formula.
The number 'n' would be entered by the user.
I was successful in creating the Leibniz formula but I am having trouble with the latter part i.e value precise up to 'n' places.
The trouble :-
As more and more terms will continue to add to it, it's digits will keep on changing so how to tell if a particular digit has stopped changing despite the addition of more terms.
The code written so far :-
#include<iostream>
using namespace std;
int main()
{
float s=0;
int w=-1;
for(float i=1;;i=i+2)
{
w=w*(-1);
s=s+w*(1/i);
cout<<s<<endl;
}
return 0;
}
It would be great if things would be kept simple since I am just a beginner at C++.
Thank you very much :)

Since you want to compute Pi up to arbitrary nth digit, you want a library for working with big float numbers; see
C++ library for big float numbers
the real trouble is that Leibniz formula is not useful in the context. The actual precision achieved can be estimated as the last term in the formula
Pi / 4 = 1/1 - 1/3 + 1/5 - 1/7 + ... + (-1)**(n + 1) * 1 / (2 * n - 1) + ...
If, for intance, you want Pi up to 100th digit it means that the residual (and the last term) should be less than 1e-100:
1 / (2 * n - 1) < 1e-100
2 * n - 1 > 1e100
n > 5e99
And as you can see 5e99 loops is by far too much for the modern (super-)computers

Related

trying to solve 8th project euler number 8 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This is the link of the problem.
https://projecteuler.net/problem=8
below is my code.
#include <stdio.h>
int main() {
long i,sum;
long temp = 0;
long arr[1000] = {
// Increasingly large number is ommitted//
// I just add ',' between each numbers//};
for(i=0; i<988; i++){
sum = arr[i]*arr[i+1]*arr[i+2]*arr[i+3]*arr[i+4]*arr[i+5]*arr[i+6]
*arr[i+7]*arr[i+8]*arr[i+9]*arr[i+10]*arr[i+11]*arr[i+12];
if(temp<sum){
temp = sum;
}
}
printf("%ld",temp);
return 0;
}
so I got 2091059712 which seems kind of reasonable answer.
The real problem here is, that you did not account for the size of the product. An integer is 10 digits max (2,147,483,647). So this or something alike might happen:
sum = 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9 * 9;
This gives: 2,541,865,828,329 which overflows your integer leading to undefined behaviour.
Use a larger integer type or take a different approach.
That's a brute force solution that will work fine for this size of problem.
Potential improvements:
Split the array on "0", and only test the substrings that are longer than the desired length.
Print out the numbers that ended up being the best substring. That way you can test that it actually is present in the original and the multiplication is done correctly.

Reducing Run Time C or C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can u Guys Please give me tips on how to reduce the compilation time of my c or c++ programmes...
Some basic simple techniques will be helpful.
I was solving a question through a site(https://www.codechef.com/problems/TRISQ)
The Question was :-
What is the maximum number of squares of size 2x2 that can be fit in a right angled isosceles triangle of base B.One side of the square must be parallel to the base of the isosceles triangle.Base is the shortest side of the triangle.
First line contains T, the number of test cases.
Each of the following T lines contains 1 integer B.
Output exactly T lines, each line containing the required answer.
Sample Input
11
1
2
3
4
5
6
7
8
9
10
11
Sample Output
0
0
0
1
1
3
3
6
6
10
10
MY CODE
#include<iostream>
using namespace std;
int main()
{
int T,N,a,i,j;
cin>>T;
while(T--)
{
a=0;
cin>>N;
N=N/2;
N--;
j=N;
for(i=0;i<j;i++)
{
a+=N;
N--;
}
cout<<a<<endl;
}
}
So how do u guys think that this code (for eg) can be edited for better compilation time?
First profile.
Second, turn up optimizations levels on you compiler.
Thirdly, replace your for loop with multiplication / algebra. For example, the line
a+=N
is the fundamental basis for multiplication (repetitive addition), and the loop can be replaced by:
a += j * N; N -= j;
Replacing the loop will speed up your program (if your compiler hasn't already replaced the loop).
Printing the assembly language for the function will show how the compiler applied optimizations.
Edit 1:
Less code means a faster build time as well. I don't know if time difference in building is measurable.

Fast input method of number in c/c++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
inline int input()
{
int c;
int n = 0;
while ((c = getchar_unlocked()) >= '0' && c <= '9')
{
// n = 10 * n + (c - '0');
n = (n << 3) + ( n << 1 ) + c - '0';
}
return n;
}
Can someone explain how this way of inputting the number is working and how it is the fast way to input a number?
Compilers are generally very stupid, and have no understanding of the logic you're trying to implement. Moreover, they're often written by less-than-competent people who don't understand much of modern hardware.
The author of the code has realized this, and cleverly analyzed that 10 is the same as 8 + 2, and that 8 and 2 are both powers of two. For the flourish, he proceeded to turn the mathematics of exponentials into native, bitwise hardware instructions. This combination of mathematics and deep understanding of the hardware leads him to factor 10 * x as 8 * x + 2 * x and express the result in terms of instructions that are far more optimal than the naive "stupid multiplication" that would otherwise have taken place. Naturally, such optimizations are far beyond the reach of any kind of technology and cannot possibly be performed automatically.
The result is a vastly improved method of multiplying a number by ten.
Patent pending.
n << 3 equals n * 8
n << 1 equals n * 2
i.e. (n << 3) + ( n << 1 ) equals 10 * n
bitwise shift is faster than multiplication, though I'm not sure the whole thing should be faster.

Translation of number into strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to know whether there is a way in which we can print the number alphabetically i.e
123 should be printed as one two three.
The only condition is that we should not reverse the number and we should not use array.
I only know these two ways:
"Reverse the number", that is, taking the last digit and cutting it off. For each cut-off digit, one can use an array to look up the correct string.
using switch and a lot of cases
Any ideas?
for hundreds place:
int hundreds = my_num / 100 //Needs "/", NOT "%"
if(hundreds == 0)
cout << "zero";
else if(hundreds == 1)
cout << "one";
//repeat for 2-9
This process could be tweaked to do the other digits as well. It is also worth mentioning that the if/else block a) could be done with a switch/case if preferred, and b) could pretty easily be made into a separate function to avoid having to repeat the block of code over and over, I just wrote out as much as I did for clarity's sake. Note that this assumes the number you're "translating" is an integer. With integers the "/" operator will return the full quotient WITHOUT the remainder, e.g. 123 / 100 = 1, not 1.23
Not necessarily the easiest route, but you can make a function, say DigitToWord which will take a digit 0, 1, 2, ...etc to its word with a switch statement. Then I recommend using a for loop over the number, continuously dividing by 10 and taking the mod for the loop:
int num; //my number i want to print
int div = pow(10, (int)log10(num)); //find the largest power of 10 smaller than num
while(num > 0) {
int remainder = num%div;
int digit = num/div;
DigitToWord();
num = remainder;
}

C++ - Array Math [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to do this equation into C++
root[0] = root [0] - F(root[0])/ root[0] - root[1] * root[0] - root[2] * root[0] - root[3]
in this examples there are only 3, changes by user input.
The program is trying to solve polynomial equations hopefully this is enough information.
I have got the top of the Equation work here is what I am came up with:
complex<double> top, bottom;
top = (complex<double>)coefficientArray[1] * (pow (rootArray[0], Degree));
rootArray[0] = rootArray[0] - (top/bottom);
Solving linear equations is alot faster using:
The coefficient matrix
An unknown variable matrix (to which you will solve)
The answer matrix
To find the roots of higher grade equations -with an approximation error- you should use the Newton-Raphson method.
I think what you want is something like
int highestPower = // whatever the user input says is the highest power
for (int i=1; i<=highestPower; i++) {
bottom += root[i] * pow(root[0], i);
}