Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NewMath
- {
- //iterative pow() method!
- public static int pow(int base, int exp)
- {
- int prod = 1;
- for(int i=0; i<exp; i++)
- {
- prod*=base;
- }
- return prod;
- }
- //how the Math class does pow()!!
- public static double pow(double base, double exp)
- {
- return Math.exp(exp*Math.log(base));
- }
- //recursive pow() method!!!
- public static double pow(double base, int exp)
- {
- if(exp==0)
- {
- return 1;
- }
- return base*pow(base,exp-1);
- }
- //another recursive method, now for factorials
- public static int fact(int num)
- {
- if(num==0)
- {
- return 1;
- }
- return num*fact(num-1);
- }
- //recursing in public! evaluate nCr
- public static int pascal(int row, int col)
- {
- if(col==0 || row==col)
- {
- return 1;
- }
- return pascal(row-1,col)+pascal(row-1,col-1);
- }
- //recursing in public! find one row of pascal's triangle
- public static String pascalRow(int n)
- {
- String temp = "";
- for(int i=0; i<=n; i++)
- {
- temp += pascal(n,i) + "\t";
- }
- return temp;
- }
- //recursing in public! print out pascal's triangle
- public static String pascalTri(int n)
- {
- String temp = "";
- for(int i=0; i<=n; i++)
- {
- temp += pascalRow(i) + "\n";
- }
- return temp;
- }
- //find fibonacci sequence terms
- public static int fib(int n)
- {
- if(n==0 || n==1){return 1;}
- return fib(n-1)+fib(n-2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement