Skip to content

Develop #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
109 changes: 106 additions & 3 deletions src/main/java/sbu/cs/CalculatePi/PiCalculator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package sbu.cs.CalculatePi;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class PiCalculator {

/**
Expand All @@ -11,17 +18,113 @@ public class PiCalculator {
* Create as many classes and threads as you need.
* Your code must pass all of the test cases provided in the test folder.

* @param floatingPoint the exact number of digits after the floating point
* @param "floatingPoint" the exact number of digits after the floating point
* @return pi in string format (the string representation of the BigDecimal object)
*/

public String calculate(int floatingPoint)
public static class Calculatepi implements Runnable {
int n;
MathContext mc = new MathContext(10000);
public Calculatepi (int n) {
this.n = n;
}
@Override
public void run() {
BigDecimal sign = new BigDecimal(1);
if (n%2 == 1) {
sign = new BigDecimal(-1);
}
//BigDecimal n = new BigDecimal(m);
BigDecimal numerator = new BigDecimal(1);
BigDecimal denominator = new BigDecimal(1);
BigDecimal a = factorial(6*n);
BigDecimal b = new BigDecimal((545140134*n)+13591409);
numerator = numerator.multiply(sign,mc);
numerator = numerator.multiply(a,mc);
numerator = numerator.multiply(b,mc);
BigDecimal c = factorial(3*n);
BigDecimal d =factorial(n);
d = d.pow(3);
BigDecimal e = new BigDecimal(640320);
e = e.pow(3*n);
BigDecimal f = new BigDecimal(640320);
f = f.pow(3);
MathContext g = new MathContext(10000);
f = f.sqrt(g);
denominator = denominator.multiply(c,mc);
denominator = denominator.multiply(d,mc);
denominator = denominator.multiply(e,mc);
denominator = denominator.multiply(f,mc);

MathContext mc = new MathContext(10000);
BigDecimal result = numerator.divide(denominator, mc);
addTosum(result);
}
public BigDecimal factorial(int k) {
BigDecimal temp = new BigDecimal(1);
for (int i = 1; i <= k; i++) {
temp = temp.multiply(new BigDecimal(i));
}
return temp;
}
}
public static BigDecimal sum;

public static synchronized void addTosum(BigDecimal value) {
sum = sum.add(value);
}
public static String calculate(int floatingPoint)
{
// TODO
return null;
//MathContext mc = new MathContext(floatingPoint);
ExecutorService threadpool = Executors.newFixedThreadPool(10);
sum = new BigDecimal(0);

for (int i = 0; i < 1000; i++) {
Calculatepi task = new Calculatepi(i);
threadpool.execute(task);
}
threadpool.shutdown();

try {
threadpool.awaitTermination(1000000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
BigDecimal factor = new BigDecimal(12);
MathContext mc = new MathContext(10000);
sum = sum.multiply(factor,mc);
sum = sum.pow(-1, mc);
sum = sum.setScale(floatingPoint, RoundingMode.DOWN);
String pi = String.valueOf(sum);
pi = pi.substring(0,floatingPoint+2);
return pi;
}

public static void main(String[] args) {
// Use the main function to test the code yourself
/*ExecutorService threadpool = Executors.newFixedThreadPool(6);
sum = new BigDecimal(0);

for (int i = 0; i < 1000; i++) {
Calculatepi task = new Calculatepi(i);
threadpool.execute(task);
}
threadpool.shutdown();

try {
threadpool.awaitTermination(100000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
BigDecimal factor = new BigDecimal(12);
MathContext mc = new MathContext(10000);
sum = sum.multiply(factor);
sum = sum.pow(-1, mc);
sum = sum.setScale(2, RoundingMode.DOWN);
String pi = String.valueOf(sum);
System.out.println(pi);
*/

}
}
37 changes: 37 additions & 0 deletions src/main/java/sbu/cs/CalculatePi/Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## PiCalculator

Among the sites, 7learn.com site explained 4 algorithms well

* Monte-Carlo method to calculate pi

* A calculation method for the Pi number

* The infinite sum method in calculating Pi

* An improved infinite sum method for calculating Pi

Among these four algorithms, the last algorithm was the most optimal and accurate algorithm

## About this algorithm:
### Chudnovsky algorithm

Ramanujan wrote two formulas to calculate pi. The Chanowski brothers,
Gregory (1952) and Dioud (1947) improved Ramanujan's early formulas.

![img.png](img.png)

The Chudnovsky algorithm is a fast method for calculating the digits of π, based on Ramanujan's π formulae. Published by the Chudnovsky brothers in 1988, it was used to calculate π to a billion decimal places.

<<<<<<< HEAD
It was used in the world record calculations of 2.7 trillion digits of π in December 2009, 10 trillion digits in October 2011, 22.4 trillion digits in November 2016, 31.4 trillion digits in September 2018–January 2019, 50 trillion digits on January 29, 2020, 62.8 trillion digits on August 14, 2021, 100 trillion digits on March 21, 2022, and 105 trillion digits on March 14, 2024.

## Semaphoro

By searching the internet, sites linkedin.com and geeksforgeeks.org provided a good definition of semaphore.

* linkedin: In Java's concurrency API, a semaphore is another synchronization tool that simultaneously controls the number of threads accessing a particular resource or section of code. It manages a set of permits; threads must acquire a permit before proceeding. If a permit is available, the thread acquires it and continues execution. If not, the thread is blocked until a permit becomes available or interrupted.

* geeksforgeeks: A semaphore controls access to a shared resource through the use of a counter. If the counter is greater than zero, then access is allowed. If it is zero, then access is denied. What the counter is counting are permits that allow access to the shared resource. Thus, to access the resource, a thread must be granted a permit from the semaphore.
=======
It was used in the world record calculations of 2.7 trillion digits of π in December 2009, 10 trillion digits in October 2011, 22.4 trillion digits in November 2016, 31.4 trillion digits in September 2018–January 2019, 50 trillion digits on January 29, 2020, 62.8 trillion digits on August 14, 2021, 100 trillion digits on March 21, 2022, and 105 trillion digits on March 14, 2024.
>>>>>>> origin/develop
Binary file added src/main/java/sbu/cs/CalculatePi/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 11 additions & 2 deletions src/main/java/sbu/cs/Semaphore/Controller.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package sbu.cs.Semaphore;

import java.util.concurrent.Semaphore;

public class Controller {

public static final Semaphore sem = new Semaphore(2);

/**
* A number of operators are trying to access a resource in parallel to each other.
* A maximum of 2 operators should access the resource concurrently. Once 2 operators have entered
Expand All @@ -18,7 +22,7 @@ public class Controller {
* Note that every time a thread accesses the resource, you must print its Name and the System Time.
*/

public static void main(String[] args) {
public static void main(String[] args) throws InterruptedException {
Operator operator1 = new Operator("operator1");
Operator operator2 = new Operator("operator2");
Operator operator3 = new Operator("operator3");
Expand All @@ -35,8 +39,13 @@ public static void main(String[] args) {
TODO
Use a Semaphore to solve the synchronization problem.
Every time a thread accesses the resource, print its Name and the current System Time.
Combine the 4 resulting blocks to create the final matrix product and return it.
*/

operator1.join();
operator2.join();
operator3.join();
operator4.join();
operator5.join();

}
}
30 changes: 23 additions & 7 deletions src/main/java/sbu/cs/Semaphore/Operator.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package sbu.cs.Semaphore;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class Operator extends Thread {

public Operator(String name) {
Expand All @@ -8,14 +11,27 @@ public Operator(String name) {

@Override
public void run() {
for (int i = 0; i < 10; i++)
{
Resource.accessResource(); // critical section - a Maximum of 2 operators can access the resource concurrently
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
try {
Controller.sem.acquire();
for (int i = 0; i < 10; i++) {
Resource.accessResource(); // critical section - a Maximum of 2 operators can access the resource concurrently
try {
sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
LocalTime currentTime = LocalTime.now ();

DateTimeFormatter formatter = DateTimeFormatter.ofPattern ("HH:mm:ss");
System.out.println (Thread.currentThread ().getName () + " accessed the resource at " + currentTime.format (formatter));
}
catch (InterruptedException e) {
System.out.println(e.getMessage());
}
finally {
Controller.sem.release();
}
}
}