Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
target
.DS_Store
.idea
21 changes: 16 additions & 5 deletions src/main/java/edu/eci/arsw/math/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
*/
package edu.eci.arsw.math;

import java.util.ArrayList;
import java.util.Arrays;
import edu.eci.arsw.threads.PiThread;

/**
*
Expand All @@ -14,9 +16,19 @@
public class Main {

public static void main(String a[]) {
System.out.println(bytesToHex(PiDigits.getDigits(0, 10)));
System.out.println(bytesToHex(PiDigits.getDigits(1, 100)));
System.out.println(bytesToHex(PiDigits.getDigits(1, 1000000)));
int numThreads = 1; // Varia para cada experimento (1,8,16,200,500)
int numDigits = 1000000;

System.out.println("Ejecutando con " + numThreads + " hilos...");

long startTime = System.nanoTime(); // Iniciar medición de tiempo
byte[] piDigits = PiDigits.getDigits(0, numDigits, numThreads);
long endTime = System.nanoTime(); // Finalizar medición de tiempo

double elapsedTime = (endTime - startTime) / 1e9; // Convertir a segundos
System.out.println("Tiempo de ejecución: " + elapsedTime + " segundos");

System.out.println(bytesToHex(piDigits));
}

private final static char[] hexArray = "0123456789ABCDEF".toCharArray();
Expand All @@ -35,5 +47,4 @@ public static String bytesToHex(byte[] bytes) {
}
return sb.toString();
}

}
}
138 changes: 45 additions & 93 deletions src/main/java/edu/eci/arsw/math/PiDigits.java
Original file line number Diff line number Diff line change
@@ -1,113 +1,65 @@
package edu.eci.arsw.math;

/// <summary>
/// An implementation of the Bailey-Borwein-Plouffe formula for calculating hexadecimal
/// digits of pi.
/// https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
/// *** Translated from C# code: https://github.com/mmoroney/DigitsOfPi ***
/// </summary>
public class PiDigits {

private static int DigitsPerSum = 8;
private static double Epsilon = 1e-17;
import java.util.ArrayList;
import edu.eci.arsw.threads.PiThread;

public class PiDigits {

/**
* Returns a range of hexadecimal digits of pi.
* @param start The starting location of the range.
* @param count The number of digits to return
* @return An array containing the hexadecimal digits.
*/
public static byte[] getDigits(int start, int count) {
if (start < 0) {
throw new RuntimeException("Invalid Interval");
private static ArrayList<PiThread> threads = new ArrayList<>();
private static byte[] digits;

public static byte[] getDigits(int start, int count, int threadNumber) {

digits = new byte[count];
threads.clear();

if (threadNumber > count) {
threadNumber = count;
}

if (count < 0) {
throw new RuntimeException("Invalid Interval");
}
int digitsPerThread = count / threadNumber;
int remainingDigits = count % threadNumber;
int currentStart = start;

byte[] digits = new byte[count];
double sum = 0;
System.out.println("Iniciando cálculo con " + threadNumber + " hilos...");

for (int i = 0; i < count; i++) {
if (i % DigitsPerSum == 0) {
sum = 4 * sum(1, start)
- 2 * sum(4, start)
- sum(5, start)
- sum(6, start);
long startTime = System.nanoTime(); // Medir tiempo de inicio

start += DigitsPerSum;
for (int i = 0; i < threadNumber; i++) {
int digitsForThisThread = digitsPerThread;
if (i == threadNumber - 1) {
digitsForThisThread += remainingDigits;
}

sum = 16 * (sum - Math.floor(sum));
digits[i] = (byte) sum;
int threadEnd = currentStart + digitsForThisThread;

PiThread thread = new PiThread(currentStart, threadEnd);
threads.add(thread);
thread.start();

currentStart = threadEnd;
}

return digits;
}

/// <summary>
/// Returns the sum of 16^(n - k)/(8 * k + m) from 0 to k.
/// </summary>
/// <param name="m"></param>
/// <param name="n"></param>
/// <returns></returns>
private static double sum(int m, int n) {
double sum = 0;
int d = m;
int power = n;

while (true) {
double term;

if (power > 0) {
term = (double) hexExponentModulo(power, d) / d;
} else {
term = Math.pow(16, power) / d;
if (term < Epsilon) {
break;
}
try {
for (PiThread thread : threads) {
thread.join();
}

sum += term;
power--;
d += 8;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Calculation interrupted", e);
}

return sum;
}

/// <summary>
/// Return 16^p mod m.
/// </summary>
/// <param name="p"></param>
/// <param name="m"></param>
/// <returns></returns>
private static int hexExponentModulo(int p, int m) {
int power = 1;
while (power * 2 <= p) {
power *= 2;
}

int result = 1;

while (power > 0) {
if (p >= power) {
result *= 16;
result %= m;
p -= power;
}

power /= 2;
long endTime = System.nanoTime(); // Medir tiempo de finalización
double elapsedTime = (endTime - startTime) / 1e9;
System.out.println("Tiempo total de ejecución en PiDigits: " + elapsedTime + " segundos");

if (power > 0) {
result *= result;
result %= m;
}
int offset = 0;
for (PiThread thread : threads) {
byte[] threadDigits = thread.getDigits();
System.arraycopy(threadDigits, 0, digits, offset, threadDigits.length);
offset += threadDigits.length;
}

return result;
return digits;
}

}
}
35 changes: 31 additions & 4 deletions src/main/java/edu/eci/arsw/threads/CountThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,38 @@
* and open the template in the editor.
*/
package edu.eci.arsw.threads;
import java.lang.Thread;


/**
*
* @author hcadavid
* @author Team 2 (Samuel, Juan, Valentina)
*/
public class CountThread {

}
public class CountThread extends Thread {

private int A;
private int B;

public CountThread(int A, int B){
this.A = A;
this.B = B;
}

public void run(){
printed();
}

public int getA(){
return A;
}

public int getB(){
return B;
}

public void printed(){
for (int i = getA(); i<= getB();i++ ){
System.out.println(i);
}
}
}
8 changes: 7 additions & 1 deletion src/main/java/edu/eci/arsw/threads/CountThreadsMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

/**
*
* @author hcadavid
* @author Team 2 (Samuel, Juan, Valentina)
*/
public class CountThreadsMain {

public static void main(String a[]){
CountThread countThread1 = new CountThread(0, 99);
CountThread countThread2 = new CountThread(99, 199);
CountThread countThread3= new CountThread(199, 299);

countThread1.run();
countThread2.run();
countThread3.run();
}

}
101 changes: 101 additions & 0 deletions src/main/java/edu/eci/arsw/threads/PiThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.eci.arsw.threads;

public class PiThread extends Thread {

private static int DigitsPerSum = 8;
private static double Epsilon = 1e-17;

private byte[] digits;
private int start;
private int count;

public PiThread(int start, int end) {
this.start = start;
this.count = end - start;
this.digits = new byte[this.count];
}

public void run() {
piDigits();
}

public byte[] piDigits() {
if (start < 0 || count < 0) {
throw new RuntimeException("Invalid Interval");
}

double sum = 0;
int currentPosition = start;

for (int i = 0; i < count; i++) {
if (i % DigitsPerSum == 0) {
sum = 4 * sum(1, currentPosition)
- 2 * sum(4, currentPosition)
- sum(5, currentPosition)
- sum(6, currentPosition);

currentPosition += DigitsPerSum;
}

sum = 16 * (sum - Math.floor(sum));
digits[i] = (byte) Math.floor(sum);
}

return digits;
}

public byte[] getDigits() {
return digits;
}

private static double sum(int m, int n) {
double sum = 0;
int d = m;
int power = n;

while (true) {
double term;

if (power > 0) {
term = (double) hexExponentModulo(power, d) / d;
} else {
term = Math.pow(16, power) / d;
if (term < Epsilon) {
break;
}
}

sum += term;
power--;
d += 8;
}

return sum;
}

private static int hexExponentModulo(int p, int m) {
int power = 1;
while (power * 2 <= p) {
power *= 2;
}

int result = 1;

while (power > 0) {
if (p >= power) {
result *= 16;
result %= m;
p -= power;
}

power /= 2;

if (power > 0) {
result *= result;
result %= m;
}
}

return result;
}
}
Loading