[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

The new fizzbuzz

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 75
Thread images: 7

File: pi_digits.jpg (104KB, 800x600px) Image search: [Google]
pi_digits.jpg
104KB, 800x600px
Is /g/ smart enough to write a function to estimate pi ? Provide the precision is you can.
>>
File: AAAAAAAAAAAA.gif (47KB, 250x194px) Image search: [Google]
AAAAAAAAAAAA.gif
47KB, 250x194px
double pi(void) {
return atan(1) * 4;
}


can i have job now??
>>
>>59867602
Nice one, now try without any trigonometric function.
>>
>>59867675
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 <stdio.h>
#include <math.h>

double pi_series(long n) {
double pi = 0;
for(long i=1; i<=n; i++) {
pi += 1./(double)(i*i);
}
pi = sqrt(pi*6.);
return pi;
}

void main(void) {
printf("%.10f\n", pi_series(100000000));
}

Error is in the order of 1/N, so 1e-8 with N=100000000.
>>
>>59867675
https://cs.uwaterloo.ca/~alopez-o/math-faq/mathtext/node12.html
the chudnovsky formula is capable of arbitrary precision
>>
>>59867740
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.
>>
File: piet pi codel size 3.png (4KB, 417x312px) Image search: [Google]
piet pi codel size 3.png
4KB, 417x312px
It's not mine but I really like this one.
>>
>>59867805
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.
>>
>>59867817
>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)
>>
>>59867555
int t = 0;
var ran = new Random();
while(t!=Math.Pi)
{
t = ran.NextDouble() * ran.Next(0,10);
}
return t;
>>
>>59867859
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<=1.
>>
[
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
>>
>>59867860
this would take fucking forever
>>
>>59867775
No time to waste.
>>
>>59867894
What is that language?
>>
>>59867894
what the fuck language is this
>>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

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
>>
>>59867965
using the Bailey-Borwein-Plouffe formula
>>
>>59867916
Let's make it l337:
0x6FE626/0x239AAF
>>
>>59867947
>>59867952

$ dc pi.dc
100000
3.13372
>>
>>59867976
kek
>>
>>59867976
I think there's some rounding error here
>>
>>59867837
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?
>>
>>59868033
CSR matrix-vector multiplication could be an interesting exercice. Just give the definition of the CSR format, and profit.
>>
>>59867555
>>>/homework/
>>
>not just using the Taylor series for pi
>>
>>59867555
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
>>
>>59867555
>estimate
You probably mean approximate.
Use your words with precision.
>>
>>59867555
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
>>
>>59867976
Close enough.
>>
File: result.png (17KB, 945x88px) Image search: [Google]
result.png
17KB, 945x88px
>>59869924
The stupid spam filter thinks the number pi is spam.
>>
File: to1000.png (55KB, 956x255px) Image search: [Google]
to1000.png
55KB, 956x255px
>>59869992
And to 1000 digits for shit and giggles.
>>
File: Untitled.png (18KB, 641x186px) Image search: [Google]
Untitled.png
18KB, 641x186px
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;
>>
>>59870130
forgot, its 792 decimals
>>
print 22/7
>>
>draw circle with radius r
>measure the circumference
>divide the circumference by 2*r
>>
>>59870704
>draw circle with circumference c
>measure the radius
>divide c by 2*the radius
>>
>>59867892
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 <= 1 then count = count + 1

return 4*count/samples
>>
>>59867740
If the problem is to estimate the pi, then using a built-in pi in the atan function is an incorrect answer.
>>
>>59867892
I tried to write that in C, which I'm a total beginner in, but I it's really shit imo
#include <stdio.h>
#include <stdlib.h>

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) <= 1) {
samplesIn += 1;
}
}
printf("%f\n", 4*((float)samplesIn/samples));
return 0;
}


3.141745
>>
>>59867926
Int != Double
>>
>>59867555
Just use Leibniz...
>>
>>59868107
you could use a taylor series of an inverse trig function
>>
>>59872167
Exhaustive approach is way faster and has arbitrary precision btw:
#include <stdio.h>

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;
}
>>
File: PIextremlyFAST.png (7KB, 310x79px) Image search: [Google]
PIextremlyFAST.png
7KB, 310x79px
>>59872292
Fuck this. Ramanujan is way faster.
>>
>>59867943
>No time to waste
>Estimate pi function
>>
print 3
>>
>>59872351
> 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.)
>>
>>59870130
>while (...)
Ummmm.
>while (true)
Uuuuuuh.
>>
>>59874794
while (true) loop is objectively superior to loop
>>
>>59874935
Pretty sure it's worthy of a warning
>>
>>59874935
Can you develop your point please ?
>>
>>59867675
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;
}
>>
>>59867916
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 <= 1.0 then
succ success
else
success in
loop (succ total) success
;;

let main () =
Random.self_init ();
loop 0 0
;;

let () = main ();;
>>
self.implement("https://www.google.se/?l#q=what%27s+a+good+algorithm+for+pi")
>>
#!/usr/bin/perl

my $radius=100;
my $pi;
my $count;
my $x,$y;

for ($x=-$radius; $x<$radius; $x++){
for ($y=-$radius; $y<$radius; $y++){
if(sqrt($x**2 + $y**2) < $radius){
$count++;
}
}
}

$pi = $count/(($radius)**2);

print"$pi";


returns 3,1397
>>
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)));

>>
>>59874526
kek
>>
J

<[email protected]^25
31415926535897932384626433
>>
>>59875992
HIRE THIS MAN
>>
>>59876201
>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;
}
>>
>>59877500
let rec gcd a = function
| 0 -> a
| b -> gcd b (a mod b)
;;
>>
>>59867555
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 /g/ seems to be allergic to maths I think it's an interesting way to solve it
>>
If I had pi I could estimate it
Thread posts: 75
Thread images: 7


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.