# March 14th Pi Day Challenge

In a few days it will be Pi Day again.

So here is the challenge. Show how you can calculate Pi in the most beautiful, ugliest, smallest, biggest, etc, etc way possible with App Inventor.

Here you have some examples:

Now it is up to you.................

Maybe you can make a game around calculating Pi. Maybe you can also tell the history of Pi.

Where would we be without pie..... huh Pi

10 Likes

Can I make an extension about to complete the challenge?

You can do anything you want. You can even use Javascript, Python, PHP or anything else as long as it also uses App Inventor and is made to celebrate Pi Day.

2 Likes

My little car always has Pi Day. Look at the license plate. But i can not enter that for the challenge because it doesn't use App Inventor............ yet.....

6 Likes

I like the Egyptian Octagon method:

``````((2r)^2  * 7)/9  where r=1
``````

slightly closer:

``````((2r)^2  * 7.075)/9  where r=1
``````

To be honest this is actually a calculator for the area of a circle WITHOUT using Pi, just using a value of r=1 returns an approximation of Pi. They built the pyramids with stuff like this.

5 Likes

There are still a lot of methods to calculate Pi.

In Java, Math.PI only returns 15 digits. If you want to use an even more precise count, you need something more advanced.

1 Like

I would like to see everything, especially in App Inventor blocks.

3 Likes

Here is the first extension - Pi.

GotPi

This event is fired when the system has got the pi value from the GetPi block successfully.

Parameters: result = text

GetPi

Generates digits of pi according to the accuracy parameter. When the accuracy parameter increases, so does the number of digits and it will become closer to the real pi.

Parameters: accuracy = number (int)

What is accuracy?

These are the tests of the accuracy parameter.

3: 2933

10: 3140

25: 31415926

60: 31415926535897932379

100: 31415926535897932384626433832787

Download AIX: com.gordonlu.pi.aix (7.0 KB)

For the source code, here you go.

``````package com.gordonlu.pi;

import android.app.Activity;
import android.content.Context;
import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.EventDispatcher;

import java.math.*;
import java.lang.*;
import java.lang.StringBuffer;
import java.util.Arrays;

@DesignerComponent(
version = 1,
description = "A non-visible extension that is used to celebrate Pi day by calculating digits of pi.",
category = ComponentCategory.EXTENSION,
nonVisible = true,
iconName = "https://docs.google.com/drawings/d/e/2PACX-1vQCI87PHLBF0jb8QWyYmIRQSjjNW3EFXf-qpsWCvBYkUQ9vEgPAB8SpxcMpblxNpbIYrjCjLrRLIU2c/pub?w=16&h=16")

@SimpleObject(external = true)
//Libraries
@UsesLibraries(libraries = "")
//Permissions
@UsesPermissions(permissionNames = "")

public class Pi extends AndroidNonvisibleComponent {

//Activity and Context
private Context context;
private Activity activity;

public static BigDecimal pi =  BigDecimal.ZERO;
public static BigDecimal denom1 =  BigDecimal.ONE;
public static BigDecimal denom2 =  BigDecimal.ONE;
public static BigDecimal term1 =  BigDecimal.ZERO;
public static BigDecimal term2 =  BigDecimal.ZERO;

public Pi(ComponentContainer container){
super(container.\$form());
this.activity = container.\$context();
this.context = container.\$context();
}

@SimpleFunction(description = "Generates digits of pi according to the accuracy parameter. When the accuracy parameter increases, " +
"so does the number of digits and it will become closer to the real pi.")
public void GetPi(int accuracy){
String p = pi_digits(accuracy);
GotPi(p);
}
public static String pi_digits(int digits){
StringBuffer pi = new StringBuffer();
int[] arr = new int[digits + 1];
int carry = 0;

for (int i = 0; i <= digits; ++i)
arr[i] = ARRINIT;

for (int i = digits; i > 0; i-= 14) {
int sum = 0;
for (int j = i; j > 0; --j) {
sum = sum * j + SCALE * arr[j];
arr[j] = sum % (j * 2 - 1);
sum /= j * 2 - 1;
}

pi.append(String.format("%04d", carry + sum / SCALE));
carry = sum % SCALE;
}
return pi.toString();
}
private static final int SCALE = 10000;
private static final int ARRINIT = 2000;
@SimpleEvent(description = "This event is fired when the system has got the pi value from the GetPi block successfully.")
public void GotPi(String result) {
EventDispatcher.dispatchEvent(this, "GotPi", result);
}
}
``````

http://www.codecodex.com/wiki/Calculate_digits_of_pi

If you want a decimal point, OK.

2 Likes

We should celebrate two Pi Days actually...

14th March for the countries following the `MM-DD` format and 22nd July for the countries following the `DD-MM` format. (31st April would have worked just fine, but then we don't have it on our calendar )

3 Likes

My brother's birthday is on July 22 - does that mean he is a math genius?

3 Likes

I like 3.14 better although i live in a 22/7 country.

2 Likes

No that means you can make him a very special Pi extension

4 Likes

You can make him a 22/7 themed pi cake on his birthday

Ah an interesting case

As a student I switch my choice depending on the situation. 3.14 for 10 and its multiples and 22/7 for 7 and its multiples. Saves you a lot of time during exams.

3 Likes

My nick name was a PI when I was in school, because I love mathematics.

5 Likes

Then Pi Day is your own special day

2 Likes

@Peter you are right, although each year my friends message me to wish PI day.

BTW here it is a code how App Inventor defines PI in runtime.scm file...

`(define *pi* 3.14159265)`

4 Likes

The Colliding Balls method would be a natural for a Canvas:

3 Likes

pi never equals 22/7. As 22/7 is a rational number but pi is irrational(non-terminating value). Since, 22/7 is easy to use, our school books and teachers uses this value.

pi is actually division of Circumference of Circle (2πr) by diameter (2r).

2 Likes

here is a solution, which uses the pi.delivery API

Taifun

6 Likes

355/113 =3. 1415929502539

2 Likes