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 :grin:

10 Likes

Can I make an extension about :pie: to complete the challenge?

You can do anything you want. :grin: 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. :joy: But i can not enter that for the challenge :crazy_face: because it doesn't use App Inventor............ yet.....

image

6 Likes

I like the Egyptian Octagon method:

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

image

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.
:wink:

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

image

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

Parameters: result = text

GetPi

image

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 :grimacing: )

3 Likes

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

3 Likes

:thinking: :grin: :+1: 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 :joy:

4 Likes

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

Ah an interesting case :joy:


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 :grin::+1::joy:

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
blocks(1)

公元480年左右,南北朝时期的数学家祖冲之进一步得出精确到小数点后7位的结果,给出不足近似值3.1415926和过剩近似值3.1415927,还得到两个近似分数值,密率355/113和约率22/7。密率是个很好的分数近似值,要取到52163/16604才能得出比355/113略准确的近似。

2 Likes