Push changes for Angel

This commit is contained in:
2024-02-23 20:26:59 -06:00
parent e74ed9dbca
commit e342f849ef
31 changed files with 1777 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.maxtask;
import java.math.BigInteger;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
*
* @author caleb
*/
public class Factorial extends RecursiveTask<Integer>
{
final int n;
Factorial(int n)
{
this.n = n;
}
@Override
protected Integer compute()
{
if (n == 1 || n <= 1)
{
return n;
}
Factorial fact = new Factorial(n - 1);
fact.fork();
return n * fact.join();
}
public static void main(String[] args)
{
RecursiveTask<Integer> task = new Factorial(4);
ForkJoinPool pool = new ForkJoinPool();
int fact = pool.invoke(task);
System.out.println(fact);
}
}

View File

@@ -0,0 +1,58 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.maxtask;
/**
*
* @author caleb
*/
import java.math.BigInteger;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class FactorialBig extends RecursiveTask<BigInteger>
{
final int THRESHOLD = 50;
public static void main(String[] args)
{
RecursiveTask<BigInteger> task = new FactorialBig( new BigInteger( "1060"));
ForkJoinPool pool = new ForkJoinPool();
BigInteger fact = pool.invoke(task);
System.out.println(fact);
}
final BigInteger n;
FactorialBig(BigInteger n)
{
this.n = n;
}
@Override
protected BigInteger compute()
{
if (this.n.compareTo(new BigInteger(String.valueOf(THRESHOLD))) < 0)
{
if (n == BigInteger.ONE || n == BigInteger.ZERO)
return BigInteger.ONE;
long fact = 1;
//compare factorial with loop
for (int i = 2; i < THRESHOLD; ++i)
{
fact *= i;
}
return new BigInteger(String.valueOf(fact));
}
else {
//compute factorial in parallel
FactorialBig factBig = new FactorialBig(n.subtract(BigInteger.ONE));
factBig.fork();
return n.multiply(factBig.join());
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.maxtask;
/**
*
* @author caleb
*/
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
public class Fibonacci extends RecursiveTask<Integer>
{
final int n;
Fibonacci(int n)
{
this.n = n;
}
@Override
protected Integer compute()
{
if (n <= 1)
{
return n;
}
Fibonacci f1 = new Fibonacci(n - 1);
f1.fork();
Fibonacci f2 = new Fibonacci(n - 2);
return f2.compute() + f1.join();
}
public static void main(String[] args)
{
RecursiveTask<Integer> task = new Fibonacci(8);
ForkJoinPool pool = new ForkJoinPool();
int fib = pool.invoke(task);
System.out.println(fib);
}
}
class Test
{
}

View File

@@ -14,7 +14,7 @@ public class ParallelMax
public static void main(String[] args)
{
// Create a list
final int N = 20;
final int N = 999999999;
int[] list = new int[N];
for (int i = 0; i < list.length; i++)
list[i] = i;
@@ -75,21 +75,21 @@ public class ParallelMax
for (int i = low; i < high; i++)
if (list[i] > max)
max = list[i];
return new Integer(max);
return max;
}
else
{
int mid = (low + high) / 2;
RecursiveTask<Integer> left = new MaxTask(list, low, mid);
System.out.println("left: " + low +", " + high);
//System.out.println("left: " + low +", " + high);
RecursiveTask<Integer> right = new MaxTask(list, mid+1, high);
System.out.println("right: " + (mid + 1) +", " + high);
System.out.println("fork right");
//System.out.println("right: " + (mid + 1) +", " + high);
//System.out.println("fork right");
right.fork();
System.out.println("fork left");
//System.out.println("fork left");
left.fork();
return new Integer(Math.max( left.join().intValue(),
right.join().intValue())
return Math.max( left.join().intValue(),
right.join().intValue()
);
}
}