Chalk
Raj's teacher Anu uses chalk to write on the board. When the chalk reduces to 1/squareroot(n) of its original size, she keeps the chalk aside as it gets too small. She joins all the small pieces of the chalk and makes another chalk of the same size and uses it. If she uses one chalk each day, in how many days will she use the given n number of chalks?
INPUT & OUTPUT FORMAT:
Input consists of 1 integer which corresponds to the number of chalks given. Output corresponds to the number of days in which the given number of chalks will be used.
SAMPLE INPUT:
16
SAMPLE OUTPUT:
21
Answers
The Chalk Roots - Java and Python
The problem is a mathematical one, and the solution becomes very elegant once we analyze it a bit.
So, we have chalks. Raj's teacher Anu will use each of them till they are of their size.
Let us suppose that the length of each chalk is 1 unit.
So, after using chalks, we are going to be left with
Now, remember that each chalk is one unit in length. So, when Anu builds new chalks from these pieces, we will have chalks.
Remember that chalks come only in integer amounts. So, we will have to round it off with the floor function.
Finally, if is a perfect square, then is an integer and after these chalks are used, we will have:
Thus, if is an integer, then Anu can build one more chalk.
However, if , then we have a problem. The criteria to throw the chalk away is , which means the chalk goes for infinite days. Or zero days if we want to define it that way.
Since we aren't given what to do in this edge case, we will just count it as Infinite.
Finally, we can define a function.
Based on our analysis, the function definition will look like this:
Or, if we want to simplify further, we would have:
This combination of floor and ceiling functions takes care of the 0 and 1 thing. In Java, we use the library and this simply becomes:
(We need to do a typecast or Java will throw an error)
And in Python, we would have to import the module and some functions from it. And do this:
Anyways, let's finally get to the coding part.
Chalk.java
import java.util.Scanner;
public class Chalk
{
public static void main(String[] args)
{
// Create Scanner Object
Scanner sc = new Scanner(System.in);
// Take user input of n
int n = sc.nextInt();
// Close Scanner Object
sc.close();
// If n is 1, return Infinity and exit
if (n == 1)
{
System.out.println(Double.POSITIVE_INFINITY);
return;
}
// Calculate the number of days the chalks will work
int numberOfDays = (int) (n + Math.floor(Math.sqrt(n)) + Math.floor(Math.sqrt(n) / Math.ceil(Math.sqrt(n))));
// Print the answer
System.out.println(numberOfDays);
}
}
Chalk.py
from math import sqrt, floor, ceil, inf
from sys import exit
# Take user input of n
n = int(input())
# If n is 0, print 0
if n == 0:
print(0)
exit(0)
# If n is 1, print math.inf (infinity)
if n == 1:
print(inf)
exit(0)
# Calculate number of days the chalks will work
number_of_days = int(n + int(sqrt(n)) + sqrt(n) // ceil(sqrt(n)))
# Print the answer
print(number_of_days)
A few screenshots are attached for reference.