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.
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.....
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.
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.
I would like to see everything, especially in App Inventor blocks.
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.
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 )
My brother's birthday is on July 22 - does that mean he is a math genius?
I like 3.14 better although i live in a 22/7 country.
No that means you can make him a very special Pi extension
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.
My nick name was a PI when I was in school, because I love mathematics.
Then Pi Day is your own special day
@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)
The Colliding Balls method would be a natural for a Canvas:
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).
355/113 =3. 1415929502539
公元480年左右,南北朝时期的数学家祖冲之进一步得出精确到小数点后7位的结果,给出不足近似值3.1415926和过剩近似值3.1415927,还得到两个近似分数值,密率355/113和约率22/7。密率是个很好的分数近似值,要取到52163/16604才能得出比355/113略准确的近似。
Apparently, 39 decimal places is enough for most calculations in the universe, something to do with the width of a hydrogen atom.