Is Sup Forums smart enough to write a function to estimate pi ? Provide the precision is you can.
The new fizzbuzz
Other urls found in this thread:
cs.uwaterloo.ca
twitter.com
double pi(void) {
return atan(1) * 4;
}
can i have job now??
Nice one, now try without any trigonometric function.
Not even joking, if I had an applicant try to reinvent the wheel on a fucking pi function, I'd show him the door.
#include
#include
double pi_series(long n) {
double pi = 0;
for(long i=1; i
cs.uwaterloo.ca
the chudnovsky formula is capable of arbitrary precision
So, no fun allowed?
Approximating PI to an accuracy beyond what the standard library provides is a classic case of premature optimization. Show me a situation where that kind of precision is actually needed, and then we can start talking about the implementation. Demanding arbitrary accuracy "just because" is a classic sign of an inferiority complex. Such an attitude doesn't belong in real-world programming, where deadlines and correctness are far more important than perfection.
It's not mine but I really like this one.
OP here. I'm a pro as well, and you're right. Still, they are plenty of algorithms to do so and I think it's nice to see wich one people will use here.
>bar on far left pushes the value of the radius onto the stack
>circle pushes the area of the circle onto the stack
>area/(radius^2) = pi
extra stuff is to print cleanly because piet is weird.
if you bumped up the resolution of the image, you'd get more accurate because you're getting a better approximation of the area of that circle (number of pixels in it)
int t = 0;
var ran = new Random();
while(t!=Math.Pi)
{
t = ran.NextDouble() * ran.Next(0,10);
}
return t;
This one is nice. It reminds me a bit the Monte-Carlo one, where you draw uniformly distributed points on the unit square and count the number of those whose square sur is under one vs. the others, ie x^2+y^2
[
Registers:
u - routine : execute i if sum of squares less than 1
i - routine : increment register x
z - routine : iterator - execute u while n > m++
r - routine : RANDU PRNG
m - variable: number of samples
x - variable: number of samples inside circle
s - variable: seed for r
k - variable: scale for division
n - variable: number of iterations (user input)
]c
[lrx 2^ lrx 2^ + 1>i]su
[lx 1+ sx]si
[lu x lm 1+ d sm ln>z]sz
[0k ls 65539 * 2 31^ % d ss lkk 2 31 ^ /]sr
? sn
5dksk
1 ss
lzx
lx lm / 4*
p
just do something like:
return 22 / 7;
close enough
this would take fucking forever
No time to waste.
What is that language?
what the fuck language is this
#include
#include
#include
int main(void) {
int n;
double pi = 0, k;
int read = scanf("%i", &n);
if(read == 1) {
for (k = 0; k < n; k++) {
pi += (1/pow(16,k))*((4/(8*k+1))-(2/(8*k+4)) - (1/(8*k+5)) - (1/(8*k+6)));
}
printf("%.10f", pi);
} else {
printf("unable to read limit");
}
return 0;
}
for k = 20, the result is 3.1415926536
using the Bailey-Borwein-Plouffe formula
Let's make it l337:
0x6FE626/0x239AAF
3
$ dc pi.dc
100000
3.13372
kek
I think there's some rounding error here
Depending on the job, I'd stick to integer arithmetic given that you can easily optimize integer computing at dev time without having to do extended data analyze.
Matrix exercises are good to see if one knows his shit.
good enough for floating point
4 * (1..10000000).inject(0) { |s,v| s + ((v.even? ? -1 : 1) / (2.0*v -1)) }
>approximate pi
With what? Newton's method? Something else?
CSR matrix-vector multiplication could be an interesting exercice. Just give the definition of the CSR format, and profit.
>>>/homework/
>not just using the Taylor series for pi
A better question, is OP smart enough to use proper English?
void hasJob(){
double pi = 3.14;
printf("%lf\n",pi);
}
I'll take my promotion, raise, and company Ferrari in hot pink now.
>from math import pi
welcome to 2017
>estimate
You probably mean approximate.
Use your words with precision.
double pi (void) {
string known = "3.14";
int precision = 30;
var rand = new Random();
for (int i = 0; i < precision; i++)
known += rand.nextInt(0,10).ToString();
return double.Parse(known);
}
i press pi button on my calc.exe
{ echo -n "scale=100;"; seq 1 2 200 | xargs -n1 -I{} echo '(16*(1/5)^{}/{}-4*(1/239)^{}/{})';} | paste -sd-+ | bc -l
Close enough.
The stupid spam filter thinks the number pi is spam.
And to 1000 digits for shit and giggles.
havent compared to actual pi more than the first 11 decimals, should be right though
with Ada.Text_IO;
procedure piest is
type fag is array (1..2800) of Integer;
shit : fag := (others => 2000);
i,k,b,d : Integer;
c : Integer := 0;
begin
k := 2800;
while (k > 0) loop
d := 0;
i := k;
while (true) loop
d := d + shit(i)*10000;
b := 2*i-1;
shit(i) := d mod b;
d := d/b;
i := i - 1;
if (i = 0) then
exit;
end if;
d := d * i;
end loop;
Ada.Text_IO.Put(Integer'Image(c+d/10000));
c := d mod 10000;
k := k - 14;
end loop;
end piest;
forgot, its 792 decimals
print 22/7
>draw circle with radius r
>measure the circumference
>divide the circumference by 2*r
>draw circle with circumference c
>measure the radius
>divide c by 2*the radius
something like
samples = 1000
count = 0
repeat samples times:
x = random number in [0,1]
y = random number in [0,1]
if x^2 + y^2
If the problem is to estimate the pi, then using a built-in pi in the atan function is an incorrect answer.
I tried to write that in C, which I'm a total beginner in, but I it's really shit imo
#include
#include
int main(int argc, char const* argv[])
{
int samples = 100000000;
int samplesIn = 0;
float x, y;
for (int i = 0; i < samples; i++) {
// Generate
x = 2*((float) rand()/RAND_MAX)-1;
y = 2*((float) rand()/RAND_MAX)-1;
// Check if it's in unit circle
if ((x*x)+(y*y)
Int != Double
Just use Leibniz...
you could use a taylor series of an inverse trig function
Exhaustive approach is way faster and has arbitrary precision btw:
#include
int main(int argc, char const* argv[])
{
int samplesIn = 0;
double precision = 0.001;
for (double x = -1; x < 1; x=x+precision) {
for (double y = -1; y < 1; y=y+precision) {
if ((x*x)+(y*y) < 1) {
samplesIn += 1;
}
}
}
printf("%f", (float)samplesIn*precision*precision);
return 0;
}
Fuck this. Ramanujan is way faster.
>No time to waste
>Estimate pi function
print 3
> you could use a taylor series of an inverse trig function
Came here to say this. This is the obvious solution. You should be able to find a series that converges fairly quickly. Once you find it, the code is easy to write. This whole thing is really a math problem -- the programming is the trivial part. (Not that there's anything wrong with that. It's nice when that happens.)
>while (...)
Ummmm.
>while (true)
Uuuuuuh.
while (true) loop is objectively superior to loop
Pretty sure it's worthy of a warning
Can you develop your point please ?
Alrighto.
double pi(void) {
int i;
double sum;
sum = 0.0;
for (i = 0; i < 100; i++)
sum += 1 / (2*i + 1) * (i % 2 == 0 ? 1 : -1);
return sum * 4;
}
Darn it. I wanted to post that.
open Num;;
let rec loop total success =
if total mod 1_000_000 = 1 then
begin
let pi = Int 4 */ Int success // Int total in
Printf.printf "%s\n" (approx_num_fix 10 pi);
flush stdout
end;
let x = Random.float 1.0 in
let y = Random.float 1.0 in
let success =
if x *. x +. y *. y
self.implement("google.se
#!/usr/bin/perl
my $radius=100;
my $pi;
my $count;
my $x,$y;
for ($x=-$radius; $x
function coprime(a, b) {
return gcd(a, b) === 1;
}
function gcd(a, b) {
var r = Math.min(a % b, b % a);
if(r === 0) {
return Math.min(a, b);
} else {
return gcd(Math.min(a, b), r);
}
}
function rand(max, min) {
return Math.floor(Math.random() * (max - min)) + min;
}
var coprimeCount = 0;
var d, d2;
var iterations = 1000000;
for (var i = 0; i < iterations; i++) {
d = rand(100000000, 1);
d2 = rand(100000000, 1);
if(coprime(d, d2)) {
coprimeCount++;
}
}
console.log(Math.sqrt(6 / (coprimeCount / iterations)));
kek
J
HIRE THIS MAN
>gcd function
>recursions that could overflow the stack
>two divisions per iteration
>function calls to min...min every where
The fuck is wrong with this world?
long long int gcd(long long int a, long long int b)
{
while (b != 0) {
long long int t = b;
b = a%b;
a = t;
}
return a;
}
let rec gcd a = function
| 0 -> a
| b -> gcd b (a mod b)
;;
in rpl
22 enter 7 enter /
def f(x):
return 4/(1+x*x)
def pi(precision):
# \int_0^1 4/(1+x^2) dx = pi
# we'll approximate using riemann sums
pi = 0
for i in range(precision):
pi += f(i/precision)/precision
return pi
I haven't seen this one done yet, and since Sup Forums seems to be allergic to maths I think it's an interesting way to solve it
If I had pi I could estimate it