-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f0dddc2
commit 4c2ded2
Showing
7 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,36 @@ | ||
package Homework; | ||
import java.util.Dictionary; | ||
|
||
import java.math.BigInteger; | ||
import java.util.*; | ||
|
||
public class Homework01 { | ||
Dictionary d = new Dictionary(); | ||
public static int fib(int n) { | ||
|
||
private static BigInteger x = new BigInteger("1"); | ||
public static void main(String[] args) { | ||
// Scanner for terminal number input | ||
// Scanner scan = new Scanner(System.in); | ||
// System.out.println("Enter # for Fibonacci Computation: "); | ||
// int number = scan.nextInt(); | ||
// System.out.println("Fibonacci of " + number + ": " + fib(number)); | ||
|
||
// Fixed variable for debugging | ||
// String number = "34"; | ||
|
||
// Command Line Argument input | ||
String number = args[0]; | ||
int n = Integer.parseInt(number); | ||
System.out.println(fib(n)); | ||
|
||
} | ||
public static BigInteger fib(int index){ | ||
if (index == 0) return x; | ||
else if (index == 1) return x; | ||
return fibHelper(1,index,x,x); | ||
|
||
} | ||
public static BigInteger fibHelper(int location, int target, BigInteger a, BigInteger b){ | ||
if (location == target) return b; | ||
else{ | ||
return fibHelper(location + 1, target, b, a.add(b)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Bottom-Up dynamic programming - Richard Bellman 1950s | ||
// computes solution to all subproblems,starting with simplest and gradually going further | ||
package PrincetonJava.CodeSamples; | ||
|
||
/** | ||
* BottomUpFibonacci | ||
*/ | ||
public class BottomUpFibonacci { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(fibDP(34)); | ||
} | ||
|
||
public static long fibDP(int n) { | ||
long[] f = new long[n+1]; | ||
f[0] = 0; | ||
f[1] = 1; | ||
for (int i = 2; i <= n; i++) | ||
f[i] = f[i-1] + f[i+2]; | ||
return f[n]; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package PrincetonJava.CodeSamples; | ||
|
||
|
||
/** | ||
* Quadratic | ||
*/ | ||
public class Quadratic { | ||
|
||
public static void main(String[] args) { | ||
|
||
double b = Double.parseDouble(args[0]); | ||
double c = Double.parseDouble(args[1]); | ||
double discriminant = b*b - 4.0*c; | ||
double d = Math.sqrt(discriminant); | ||
System.out.println((-b + d) / 2.0); | ||
System.out.println((-b - d) / 2.0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package PrincetonJava.CodeSamples; | ||
import java.util.*; | ||
|
||
public class StandardInput { | ||
|
||
public static void main(String[] args) { | ||
Scanner test = new Scanner(System.in); | ||
System.out.println("Enter Username: "); | ||
|
||
String userName = test.nextLine(); | ||
System.out.println("Username is: " + userName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Top-Down = memoization | ||
// Cache (store) values of each subproblem. | ||
// Uses a static variable (class/global variable) | ||
// Uses an array of longs | ||
|
||
package PrincetonJava.CodeSamples; | ||
|
||
/** | ||
* TopDownFibonacci | ||
*/ | ||
public class TopDownFibonacci { | ||
|
||
public static void main(String[] args) { | ||
System.out.println(fibMemo(80)); | ||
} | ||
|
||
static long[] memo = new long[100]; | ||
|
||
public static long fibMemo(int n){ | ||
if (n==0) return 0; | ||
if (n==1) return 1; | ||
if (memo[n] == 0) | ||
memo[n] = fibMemo(n-1) + fibMemo(n-2); | ||
return memo[n]; | ||
} | ||
} |