Skip to content

Commit 69e3e6b

Browse files
committed
Update solutions
1 parent 3d5b2b4 commit 69e3e6b

11 files changed

+684
-1
lines changed

src/Chapter06/Exercise0624_Display_current_date_and_time.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public static String getDayName(int dayOfWeek) {
227227
case 6:
228228
dayName = "Friday";
229229
break;
230-
case 7:
230+
case 0:
231231
dayName = "Saturday";
232232
break;
233233
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* (Palindromic prime) A palindromic prime is a prime number and also palindromic.
3+
* For example, 131 is a prime and also a palindromic prime, as are 313 and 757.
4+
* Write a program that displays the first 120 palindromic prime numbers. Display
5+
* 10 numbers per line, separated by exactly one space, as follows:
6+
* 2 3 5 7 11 101 131 151 181 191
7+
* 313 353 373 383 727 757 787 797 919 929
8+
*
9+
* Created by Sven on 07/27/19.
10+
*/
11+
package Chapter06;
12+
13+
public class Exercise0626_Palindromic_prime {
14+
public static void main(String[] args) {
15+
int count = 0;
16+
for (int i = 0; count < 120; i++) {
17+
// It's slow to compute the last five numbers
18+
if (isPrime(i) && isPalindrome(i)) {
19+
if (count != 0 && count % 10 == 0) {
20+
System.out.println();
21+
}
22+
System.out.print(i + " ");
23+
count++;
24+
}
25+
}
26+
}
27+
28+
/**
29+
* Check whether number is prime
30+
*/
31+
public static boolean isPrime(int number) {
32+
for (int divisor = 2; divisor <= number / 2; divisor++) {
33+
if (number % divisor == 0) { // If true, number is not prime
34+
return false; // number is not a prime
35+
}
36+
}
37+
return true; // number is prime
38+
}
39+
40+
/**
41+
* Check whether number is palindromic
42+
*/
43+
public static int reverse(int number) {
44+
int reverse = 0;
45+
while (number != 0) {
46+
reverse *= 10;
47+
reverse += number % 10;
48+
number /= 10;
49+
}
50+
return reverse;
51+
}
52+
53+
public static boolean isPalindrome(int number) {
54+
return number == reverse(number);
55+
}
56+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* (Emirp) An emirp (prime spelled backward) is a nonpalindromic prime number
3+
* whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so
4+
* 17 and 71 are emirps. Write a program that displays the first 120 emirps. Display
5+
* 10 numbers per line, separated by exactly one space, as follows:
6+
* 13 17 31 37 71 73 79 97 107 113
7+
* 149 157 167 179 199 311 337 347 359 389
8+
*
9+
* Created by Sven on 07/27/19.
10+
*/
11+
package Chapter06;
12+
13+
public class Exercise0627_Emirp {
14+
public static void main(String[] args) {
15+
int count = 0;
16+
for (int i = 2; count < 120; i++) {
17+
// It's slow to compute the last five numbers
18+
if (isPrime(i) && isPrime(reverse(i))) {
19+
if (count != 0 && count % 10 == 0) {
20+
System.out.println();
21+
}
22+
System.out.print(i + " ");
23+
count++;
24+
}
25+
}
26+
}
27+
28+
public static boolean isPrime(int number) {
29+
for (int divisor = 2; divisor <= number / 2; divisor++) {
30+
if (number % divisor == 0) { // If true, number is not prime
31+
return false; // number is not a prime
32+
}
33+
}
34+
return true; // number is prime
35+
}
36+
37+
public static int reverse(int number) {
38+
int reverse = 0;
39+
while (number != 0) {
40+
reverse *= 10;
41+
reverse += number % 10;
42+
number /= 10;
43+
}
44+
return reverse;
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* (Mersenne prime) A prime number is called a Mersenne prime if it can be written
3+
* in the form 2 p - 1 for some positive integer p. Write a program that finds all
4+
* Mersenne primes with p … 31 and displays the output as follows:
5+
* p 2^p – 1
6+
* 2 3
7+
* 3 7
8+
* 5 31
9+
* ...
10+
*
11+
* Created by Sven on 07/27/19.
12+
*/
13+
package Chapter06;
14+
15+
public class Exercise0628_Mersenne_prime {
16+
public static void main(String[] args) {
17+
System.out.println("p 2^p - 1");
18+
System.out.println("----------------");
19+
for (int p = 2; p <= 31; p++) {
20+
if (isPrime((int) Math.pow(2, p) - 1)) {
21+
System.out.printf("%-9d%d\n", p, (int) Math.pow(2, p) - 1);
22+
}
23+
}
24+
}
25+
26+
public static boolean isPrime(int number) {
27+
for (int divisor = 2; divisor <= number / 2; divisor++) {
28+
if (number % divisor == 0) { // If true, number is not prime
29+
return false; // number is not a prime
30+
}
31+
}
32+
return true; // number is prime
33+
}
34+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* (Twin primes) Twin primes are a pair of prime numbers that differ by 2. For
3+
* example, 3 and 5 are twin primes, 5 and 7 are twin primes, and 11 and 13 are
4+
* twin primes. Write a program to find all twin primes less than 1,200. Display the
5+
* output as follows:
6+
* (3, 5)
7+
* (5, 7)
8+
* ...
9+
*
10+
* Created by Sven on 07/27/19.
11+
*/
12+
package Chapter06;
13+
14+
public class Exercise0629_Twin_primes {
15+
public static void main(String[] args) {
16+
for (int i = 2; i < 1200; i++) {
17+
if (isPrime(i) && isPrime(i + 2)) {
18+
System.out.println("(" + i + ", " + (i + 2) + ")");
19+
}
20+
}
21+
}
22+
23+
public static boolean isPrime(int number) {
24+
for (int divisor = 2; divisor <= number / 2; divisor++) {
25+
if (number % divisor == 0) { // If true, number is not prime
26+
return false; // number is not a prime
27+
}
28+
}
29+
return true; // number is prime
30+
}
31+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* (Game: craps) Craps is a popular dice game played in casinos. Write a program
3+
* to play a variation of the game, as follows:
4+
* Roll two dice. Each die has six faces representing values 1, 2, . . ., and 6, respectively.
5+
* Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you
6+
* lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value
7+
* (i.e., 4, 5, 6, 8, 9, or 10), a point is established. Continue to roll the dice until either
8+
* a 7 or the same point value is rolled. If 7 is rolled, you lose. Otherwise, you win.
9+
* Your program acts as a single player. Here are some sample runs.
10+
* You rolled 5 + 6 = 11
11+
* You win
12+
* You rolled 1 + 2 = 3
13+
* You lose
14+
* You rolled 4 + 4 = 8
15+
* point is 8
16+
* You rolled 6 + 2 = 8
17+
* You win
18+
* You rolled 3 + 2 = 5
19+
* point is 5
20+
* You rolled 2 + 5 = 7
21+
* You lose
22+
*
23+
* Created by Sven on 07/27/19.
24+
*/
25+
package Chapter06;
26+
27+
public class Exercise0630_Game_craps {
28+
public static void main(String[] args) {
29+
int point = rollDice();
30+
System.out.println(getStatus(point) ? "You win" : "You lose");
31+
}
32+
33+
public static boolean getStatus(int point) {
34+
if (point == 7 || point == 11) {
35+
return true;
36+
} else if (point == 2 || point == 3 || point == 12) {
37+
return false;
38+
} else {
39+
System.out.println("point is " + point);
40+
int secondPoint = rollDice();
41+
while (true) {
42+
if (secondPoint == point) {
43+
return true;
44+
} else if (secondPoint == 7) {
45+
return false;
46+
} else {
47+
System.out.println("point is " + secondPoint);
48+
secondPoint = rollDice();
49+
}
50+
}
51+
}
52+
}
53+
54+
55+
public static int rollDice() {
56+
int dice1 = (int) (Math.random() * 6 + 1);
57+
int dice2 = (int) (Math.random() * 6 + 1);
58+
int point = dice1 + dice2;
59+
System.out.println("You rolled " + dice1 + " + " + dice2 + " = " + point);
60+
return point;
61+
}
62+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/**
2+
* (Financial: credit card number validation) Credit card numbers follow certain
3+
* patterns. A credit card number must have between 13 and 16 digits. It must start
4+
* with
5+
* ■■ 4 for Visa cards
6+
* ■■ 5 for Master cards
7+
* ■■ 37 for American Express cards
8+
* ■■ 6 for Discover cards
9+
* In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card
10+
* numbers. The algorithm is useful to determine whether a card number is entered
11+
* correctly, or whether a credit card is scanned correctly by a scanner. Credit card
12+
* numbers are generated following this validity check, commonly known as the
13+
* Luhn check or the Mod 10 check, which can be described as follows (for illustration,
14+
* consider the card number 4388576018402626):
15+
* 1. Double every second digit from right to left. If doubling of a digit results in a
16+
* two-digit number, add up the two digits to get a single-digit number.
17+
* 2. Now add all single-digit numbers from Step 1.
18+
* 4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 3 7
19+
* 3. Add all digits in the odd places from right to left in the card number.
20+
* 6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 3 8
21+
* 4. Sum the results from Step 2 and Step 3.
22+
* 3 7 + 3 8 = 7 5
23+
* 5. If the result from Step 4 is divisible by 10, the card number is valid; otherwise,
24+
* it is invalid. For example, the number 4388576018402626 is invalid, but the
25+
* number 4388576018410707 is valid.
26+
* Write a program that prompts the user to enter a credit card number as a long
27+
* integer. Display whether the number is valid or invalid. Design your program to
28+
* use the following methods:
29+
* Here are sample runs of the program: (You may also implement this program by
30+
* reading the input as a string and processing the string to validate the credit card.)
31+
* Enter a credit card number as a long integer:
32+
* 4388576018410707
33+
* 4388576018410707 is valid
34+
* Enter a credit card number as a long integer:
35+
* 4388576018402626
36+
* 4388576018402626 is invalid
37+
*
38+
* Created by Sven on 07/27/19.
39+
*/
40+
package Chapter06;
41+
42+
import java.util.Scanner;
43+
44+
public class Exercise0631_Financial_credit_card_number_validation {
45+
public static void main(String[] args) {
46+
Scanner input = new Scanner(System.in);
47+
System.out.println("Enter a credit card number as a long integer:");
48+
long number = input.nextLong();
49+
System.out.println(isValid(number) ? (number + " is valid") : (number + " is invalid"));
50+
}
51+
52+
/**
53+
* Return true if the card number is valid
54+
*/
55+
public static boolean isValid(long number) {
56+
final int PREFIX_VISA = 4;
57+
final int PREFIX_MASTER = 5;
58+
final int PREFIX_AMERICAN_XP = 37;
59+
final int PREFIX_DISCOVERS = 6;
60+
if (prefixMatched(number, PREFIX_VISA) ||
61+
prefixMatched(number, PREFIX_MASTER) ||
62+
prefixMatched(number, PREFIX_AMERICAN_XP) ||
63+
prefixMatched(number, PREFIX_DISCOVERS)) {
64+
if (getSize(number) >= 13 && getSize(number) <= 16) {
65+
int sum = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
66+
return sum % 10 == 0;
67+
} else {
68+
return false;
69+
}
70+
} else {
71+
return false;
72+
}
73+
}
74+
75+
/**
76+
* Get the result from Step 2
77+
*/
78+
public static int sumOfDoubleEvenPlace(long number) {
79+
int sum = 0;
80+
while (number > 0) {
81+
number /= 10;
82+
int digit = getDigit((int) (number % 10) * 2);
83+
sum += digit;
84+
number /= 10;
85+
}
86+
return sum;
87+
}
88+
89+
/**
90+
* Return this number if it is a single digit, otherwise,
91+
* return the sum of the two digits
92+
*/
93+
public static int getDigit(int number) {
94+
return (number > 9) ? (number % 10 + number / 10) : number;
95+
}
96+
97+
/**
98+
* Return sum of odd-place digits in number
99+
*/
100+
public static int sumOfOddPlace(long number) {
101+
int sum = 0;
102+
while (number > 0) {
103+
int digit = (int) (number % 10);
104+
digit = getDigit(digit);
105+
sum += digit;
106+
number /= 100;
107+
}
108+
return sum;
109+
}
110+
111+
/**
112+
* Return true if the number d is a prefix for number
113+
*/
114+
public static boolean prefixMatched(long number, int d) {
115+
return getPrefix(number, getSize(d)) == d;
116+
}
117+
118+
/**
119+
* Return the number of digits in d
120+
*/
121+
public static int getSize(long d) {
122+
int size = 0;
123+
while (d > 0) {
124+
size++;
125+
d /= 10;
126+
}
127+
return size;
128+
}
129+
130+
/**
131+
* Return the first k number of digits from number. If the
132+
* number of digits in number is less than k, return number.
133+
*/
134+
public static long getPrefix(long number, int k) {
135+
if (getSize(number) < k) {
136+
return number;
137+
} else {
138+
long difference = getSize(number) - k;
139+
return (int) (number / Math.pow(10, difference));
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)