Consider the sequence where s(0)
and s(1)
are inputs, and s(n) = s(n-1) * s(n-2)
for all n >= 2
. I want to find the number of trailing zeros in s(n)
. We can assume the following:
n
,s(0)
, ands(1)
are given as inputsn <= 40
s(0) <= 20
s(1) <= 20
Below is my code attempt. It is not running when n
is greater than 30 (it runs for a very long time). Is there any other way to calculate the number of trailing zeroes?
public class Soroco {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BigInteger n = new BigInteger(br.readLine());
BigInteger s0 = new BigInteger(br.readLine());
BigInteger s1 = new BigInteger(br.readLine());
BigInteger num = s(n, s0, s1);
System.out.println(num);
System.out.println(countTrailingZeroes(num));
}
static BigInteger s(BigInteger n, BigInteger s0, BigInteger s1) {
if (n.equals(new BigInteger("0")))
return s0;
else if (n.equals(new BigInteger("1")))
return s1;
else {
BigInteger n1=n.subtract(new BigInteger("1"));
BigInteger n2=n.subtract(new BigInteger("2"));
BigInteger n3=s(n1, s0, s1).multiply(s(n2, s0, s1));
return n3;
}
}
static int countTrailingZeroes(BigInteger num) {
String str = String.valueOf(num);
int count = 0;
for (int i = 0; i < str.length(); i++)
if (str.charAt(i) == '0')
count++;
return count;
}
}