Skip to main content
37 votes
Accepted

PI Calculator, Interview Challenge

In an interview it usually doesn't matter if you actually solve the problem. What is most important is the way you (try to) solve it. If they don't tell you it should be the possibly fastest solution ...
t3chb0t's user avatar
  • 44.3k
26 votes

PI Calculator, Interview Challenge

I'm going to give an alternative approach to tchbot's answer There are places where SOLID prinicpals are required. I don't see this as one of them. Here, we have a simple "Are you capable of ...
Andrew Shepherd's user avatar
26 votes
Accepted

Approximating constant π² to within error

You don't need to compare prev and new during each iteration. The difference between the new and the previous sum is simply the ...
Eric Duminil's user avatar
  • 3,950
20 votes

PI Calculator, Interview Challenge

Flavio's answer addresses a part of something that matters greatly for this kind of problem: rounding errors will kill you. If you look at the expression: $$1-\frac13+\frac15-\frac17+...$$ This is ...
Floris's user avatar
  • 397
19 votes

Approximating constant π² to within error

So, you have a polynomial sequence and want to sum its terms while they are greater than a provided tolerance (i.e.: the tolerance is lower than the current computed term). This is easily expressed ...
301_Moved_Permanently's user avatar
18 votes
Accepted

IEEE 754 square root with Newton-Raphson

This answer uses pointer-casting for type-punning just to save space. In practice keep using your union (safe in ISO C99, and in C++ as a GNU and MSVC extension) or memcpy (safe in C and C++). This ...
Rainer P.'s user avatar
  • 2,622
17 votes
Accepted

Definite Integral Approximation using the Trapezoidal Method

If you are writing mathematical code in Python, then it is worth looking at NumPy, which is a library implementing (among other things) fast arithmetic operations on arrays of floating-point numbers. ...
Gareth Rees's user avatar
  • 49.6k
15 votes

Approximating constant π² to within error

Please fix the indentation (probably just a copy-pasting issue). In Python, you don't need parenthesis around the expressions for block statements like if and ...
Solomon Ucko's user avatar
  • 1,566
15 votes
Accepted

Numerical integration in C++: Newton-Cotes formulas

I wonder whether the #include "NewtonCotesFormulas.cpp" at the end of the NewtonCotesFormulas.h header file could be somehow avoided. I used it because without I'd get linking errors. Yea, ...
JDługosz's user avatar
  • 11.4k
14 votes
Accepted

Mean π: Archimedes vs. Gauss - π computation through generalized means

Convergence testing if pi == piold: break This is not usually done, because float equality has a lot of gotchas. In this case it's possible due to ...
Reinderien's user avatar
  • 62.5k
13 votes
Accepted

Python π = 1 + (1/2) + (1/3) + (1/4) - (1/5) + (1/6) + (1/7) + (1/8) + (1/9) - (1/10) ...1748 Euler

What you are approximating is $$ \pi =\sum_{m=1}^{\infty}\frac{(-1)^{s(m)}}{m},$$ where \$s(m)\$ counts the number of appearances of primes of the form \$4k+1\$ in the prime decomposition of \$m\$, ...
Martin R's user avatar
  • 23.3k
11 votes

PI Calculator, Interview Challenge

There is clearly no 'best way' to do this. From a 'Numerical Analysis' approach, you should never sum that way a series with alternating-sign terms: loss of accuracy and slow convergence speed can be ...
Flaviano de Fusco's user avatar
11 votes

Implementing numerical integration

I see a few things that may help you improve your program. Avoid pow for float The use of ...
Edward's user avatar
  • 66.5k
11 votes
Accepted

Simulating a two-body collision problem to find digits of Pi

First, this is an awesome video! Upvoted for that reason alone. :) If n=2, the program finishes within milliseconds, whereas for n=3, it takes a whopping 115 seconds. Do you know about big-O ...
Quuxplusone's user avatar
  • 19.4k
11 votes
Accepted

Calculate Pi using Monte Carlo

One could consider at least the following points: Instead of including <stdlib.h>, I'd include <cstdlib>. In ...
Juho's user avatar
  • 3,589
11 votes

Numerical integration in C++: Newton-Cotes formulas

Use an enum to give names to choices Consider using an enum, or even better an enum class, ...
G. Sliepen's user avatar
  • 58.3k
10 votes

IEEE 754 square root with Newton-Raphson

FYI, in IEEE754 sqrt is a "basic" operation that's required to be correctly-rounded (rounding error <= 0.5ulp), same as + - * /. Hardware FPUs (I think) always ...
Peter Cordes's user avatar
  • 3,346
9 votes

pure Python Bézier curve implementation

Unnecessary Generator You've got an unnecessary generator expression here: ...
AJNeufeld's user avatar
  • 33.8k
9 votes

A probability distribution function, to be called repeatedly during numerical integration

This is a possible solution to harolds open point about exp in his answer. Note that exp(x+y) = exp(x) * exp(y) holds for any ...
BlameTheBits's user avatar
8 votes

PI Calculator, Interview Challenge

Observations and Suggestions Operate on pi directly. You can eliminate the pi = 4 * number;. You are using more operations than are necessary. I prefer ...
paparazzo's user avatar
  • 6,016
8 votes

Finding the root of a function by Bisection Method

A std::vector<T> vec is an arbitrary collection of Ts. vec might contain none, one or ...
Zeta's user avatar
  • 19.3k
8 votes
Accepted

Finding the root of a function by Bisection Method

Headers and namespaces The <cmath> header is referenced, but never used; it can be removed. If we're to use std::vector, ...
Toby Speight's user avatar
  • 77.1k
8 votes

Definite Integral Approximation using the Trapezoidal Method

You can use sum with a comprehension to create part_2. You can move all the maths together if you think it looks nicer. ...
Peilonrayz's user avatar
  • 43.4k
8 votes

IEEE 754 square root with Newton-Raphson

I don't see why you define this constant yourself: #define MANTISSA_SIZE 52 Given we already assume that FLT_RADIX is 2, we ...
Toby Speight's user avatar
  • 77.1k
8 votes
Accepted

C++ class to create and evaluate Chebyshev approximations of arbitrary functions

Naming things You have a bounds checking evaluation function, operator(), and one that doesn't do bounds checking named ...
G. Sliepen's user avatar
  • 58.3k
8 votes
Accepted

Genetic algorithm to guess coefficient of a polynomial

This is not the best algorithm If the goal is to get the best coefficients for a polynomial so it fits the given points, then a polynomial regression algorithm such as ...
G. Sliepen's user avatar
  • 58.3k
7 votes

Monte Carlo estimation of π

In the code below you must realise that score is a variable that is shared between the threads. Which means that it requires synchronization, as you have omitted ...
Emily L.'s user avatar
  • 16.6k
7 votes

Implementing numerical integration

Improve robustness and readability Don't using namespace with namespaces not designed for it. Use const and ...
Toby Speight's user avatar
  • 77.1k
7 votes

A probability distribution function, to be called repeatedly during numerical integration

A typical way to reduce such cosines with an angle that is steadily counting up by the same increment, is to take a vector and rotate it step by step. That way, one cosine and one sine are calculated, ...
user555045's user avatar
  • 9,994
6 votes

Calculating pi by adding areas of thin rectangles

I see a number of things that could help you improve your program. Don't abuse using namespace std Putting using namespace std ...
Edward's user avatar
  • 66.5k

Only top scored, non community-wiki answers of a minimum length are eligible