-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recursion.java
109 lines (87 loc) · 2.95 KB
/
Recursion.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.io.*;
public class Recursion
{
public static void main( String[] args )
{
// T R I S T A R S
int rows = 5;
System.out.format("A tringle with %d rows contains %d stars\n", rows, triStars( rows ) );
// S U M D I G I T S
int number = 12345;
System.out.format("The sum of the digits in the number %d = %d\n", number, sumDigits( number ) );
// C O U N T 7 S
number = 713274772;
System.out.format("There are %d occurances of the digit 7 in the number %d\n", count7s(number), number );
// C O U N T 8 S
number = 82338828;
System.out.format("There are %d occurances of the digit 8 in the number %d\n", count8s(number), number );
// P O W E R N
int base=2,exponent=8;
System.out.format("%d to the power %d = %d\n", base, exponent, powerN(base,exponent) );
// I S S O R T E D
// perturb values as needed to test on an unsorted array
int[] array = { 7, 8, 12, 20, 21, 22, 37, 41, 55, 60, 65, 74, 83, 84, 87 };
int startingAt=0;
boolean isSorted = isSorted( array, startingAt, array.length );
System.out.print( "array: ");
for ( int i=0 ; i<array.length ; ++i ) System.out.print( array[i] + " " );
if (isSorted)
System.out.println(" is SORTED" );
else
System.out.println(" is NOT SORTED" );
// P A L I N D R O M E
String s = "stanleyyelnats"; // try with several differnt values that are or not palindromes
if ( isPalindrome( s, 0, s.length()-1 ) )
System.out.format("%s IS a palindrome\n", s );
else
System.out.format("\n%s NOT a palindrome\n", s );
} // END MAIN
// count stars in a triangle using # of rows as input
static int triStars(int rows)
{
if (rows==0) return 0;
return rows + triStars(rows-1);
}
// given a number return the sum of the digits
static int sumDigits(int n)
{
if (n==0) return 0;
return n%10 + sumDigits(n/10);
}
// given a number compute the number of 7's in that number
static int count7s(int n)
{
if (n==0) return 0;
if (n%10==7) return 1 + count7s(n/10);
return count7s(n/10);
}
// given a number count the number of 8 but if an 8 has another 8 to its immdiate left count it as 2
// the number 8802388 will return a count of 6
static int count8s(int n)
{
if (n==0) return 0;
if (n%10==8 && n%10==(n/10)%10) return 2 + count8s(n/10);
if (n%10==8) return 1 + count8s(n/10);
return count8s(n/10);
}
//compute base to the power n
static int powerN(int base, int n)
{
if (n==0) return 1;
return base * powerN(base,n-1);
}
// return true only if the array is sorted
static boolean isSorted(int array[], int i, int count )
{
if (i==count-1) return true;
if (array[i]>array[i+1]) return false;
return isSorted(array, i+1, count); //No [] after array
}
// return index of key or -1
static boolean isPalindrome(String s, int lo, int hi )
{
if (lo==hi) return true;
if (s.indexOf(lo)!=s.indexOf(hi)) return false;
return isPalindrome(s, lo+1, hi+1);
}
} // END CLASS Recursion