patternjavaMinor
Solving a practical problem involving square of root of a number
Viewed 0 times
problemsolvingnumberinvolvingrootpracticalsquare
Problem
A student has a book containing 411 pages.
She read a certain number of pages on the first day and created a rule to work out how many pages she had to read on each succeeding day.
She decided that the number of pages to be read on the next day should be equal to the square of the sum of the digits of the page she ended at.
For example, if she ended on page 36, then she should read 81 pages on the next day as this is the square of 6 + 3.
She found that on the sixth day, the number of pages she had set herself to read took her exactly to the final page of the book.
How many pages did she read each day?
Here's what I did:
Any reviews?
Numbers that end up with 411 pages, not necessarily in 6 days:
$$\begin{array}{c|c}
\text{pages read $\\$on first day}&\text{days required to$\\$ read exactly 411 pages}\\\hline
13&5\\
16&4\\
29&4\\
\huge \color{red}{61}&\huge \color{red}{6}\\
65&3\\
110&5\\
114&4\\
150&3\\
186&2\\
190&3\\
241&3\\
290&2\\
\end{array}$$
She read a certain number of pages on the first day and created a rule to work out how many pages she had to read on each succeeding day.
She decided that the number of pages to be read on the next day should be equal to the square of the sum of the digits of the page she ended at.
For example, if she ended on page 36, then she should read 81 pages on the next day as this is the square of 6 + 3.
She found that on the sixth day, the number of pages she had set herself to read took her exactly to the final page of the book.
How many pages did she read each day?
Here's what I did:
import java.util.*;
import java.lang.*;
import java.io.*;
class ADG {
public static void main(String[] args) throws java.lang.Exception {
for (int firstpage = 1; firstpage <= 411; firstpage++) {
int sum = firstpage;
int day = 1;
do {
sum += squareofroot(sum);
day++;
if (sum == 411) {
System.out.println(firstpage + "&" + day + "\\\\");
break;
}
} while (sum <= 411);
}
}
public static int squareofroot(int pnum) {
int root = 0;
int j = 0;
do {
root += pnum % 10;
pnum = pnum / 10;
j = j + 1;
} while (pnum != 0);
return (root * root);
}
}Any reviews?
Numbers that end up with 411 pages, not necessarily in 6 days:
$$\begin{array}{c|c}
\text{pages read $\\$on first day}&\text{days required to$\\$ read exactly 411 pages}\\\hline
13&5\\
16&4\\
29&4\\
\huge \color{red}{61}&\huge \color{red}{6}\\
65&3\\
110&5\\
114&4\\
150&3\\
186&2\\
190&3\\
241&3\\
290&2\\
\end{array}$$
Solution
You don't usually need to import
Did you import
You don't need to
411 is a magic number. Looking at the code alone in the future it would serve you to instantiate the value in a final field.
In java it's conventional for variables and methods to be camelCase, so as an example, in your code
Lastly, you don't really answer the question. The challenge asks
How many pages did she read on each day?`
Answering that would be trivial with your code, you actually went one further and found out different ways she could have finished the book using her method.
Suggested Implementation:
Output:
Day: 1, Pages Read: 61.
Day: 2, Pages Read: 49.
Day: 3, Pages Read: 4.
Day: 4, Pages Read: 36.
Day: 5, Pages Read: 36.
Day: 6, Pages Read: 225.
java.lang, as it is built in, but maybe this is a special case. Either way, It's best to only import the classes you need. Regardless, java.io, and java.util are unused.Did you import
java.io for exceptions? In general's, it's preferable to throw a specific Exception, this alone doesn't really say much. As it is I think it's entirely unnecessary, and wonder why you're throwing any exception in the first place.You don't need to
break when you reach the sum, your while loops condition is already checking if sum reaches 411.411 is a magic number. Looking at the code alone in the future it would serve you to instantiate the value in a final field.
In java it's conventional for variables and methods to be camelCase, so as an example, in your code
firstpage should be firstPage and squareofroot should be squareOfRoot. Additionally, class names are PascalCase, so your class ADG would be Adg (Though I'd pick a better class name), doesn't hurt to be as descriptive as possible. I'd wager if you stepped back for a few months and just saw a file named ADG you'd wonder what it is. Try to keep these standards in mind going forward, they will help keep your code readable not only to yourself but anyone else you present it to.Lastly, you don't really answer the question. The challenge asks
How many pages did she read on each day?`
Answering that would be trivial with your code, you actually went one further and found out different ways she could have finished the book using her method.
Suggested Implementation:
import java.util.Map;
import java.util.LinkedHashMap;
public class Test {
public static void main(String[] args) {
final int pageTotal = 411;
final int daysRequired = 6;
Map pagesReadPerDay = new LinkedHashMap<>();
for (int firstPage = 1; firstPage entry : pagesReadPerDay.entrySet()){
System.out.println(
"Day: " + entry.getKey() +
", Pages Read: " + entry.getValue() + "."
);
}
}
public static int squareSumDigit(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum * sum;
}
}Output:
Day: 1, Pages Read: 61.
Day: 2, Pages Read: 49.
Day: 3, Pages Read: 4.
Day: 4, Pages Read: 36.
Day: 5, Pages Read: 36.
Day: 6, Pages Read: 225.
Code Snippets
import java.util.Map;
import java.util.LinkedHashMap;
public class Test {
public static void main(String[] args) {
final int pageTotal = 411;
final int daysRequired = 6;
Map<Integer, Integer> pagesReadPerDay = new LinkedHashMap<>();
for (int firstPage = 1; firstPage <= pageTotal; firstPage++) {
int sum = firstPage;
int day = 1;
while (sum <= pageTotal) {
sum += squareSumDigit(sum);
day++;
if (sum == pageTotal && day == daysRequired) {
int pagesRead = firstPage;
int currentPage = pagesRead;
for (int onDay = 1; onDay <= daysRequired; onDay++) {
pagesReadPerDay.put(onDay, pagesRead);
pagesRead = squareSumDigit(currentPage);
currentPage += pagesRead;
}
}
}
}
for (Map.Entry<Integer, Integer> entry : pagesReadPerDay.entrySet()){
System.out.println(
"Day: " + entry.getKey() +
", Pages Read: " + entry.getValue() + "."
);
}
}
public static int squareSumDigit(int num) {
int sum = 0;
while (num > 0) {
sum += num % 10;
num /= 10;
}
return sum * sum;
}
}Context
StackExchange Code Review Q#79862, answer score: 7
Revisions (0)
No revisions yet.