Hi,
Why can't I pass validation on this task?
It's because I get the pow of 3 as a math calculation?
package com.codegym.task.task30.task3012;
/*
Derive a specific number
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
solution.createExpression(74);
}
public void createExpression(int number) {
//write your code here
System.out.println("Passed number:\n" + number);
char[] ternaryNumberString = balancedTernary(number).toCharArray();
StringBuilder res = new StringBuilder(number + " =");
for(int i = ternaryNumberString.length-1; i >=0; i--){
if(ternaryNumberString[i]=='Z'){
res.append(" - ");
} else if (ternaryNumberString[i]=='1'){
res.append(" + ");
}
if(ternaryNumberString[i]!='0') {
res.append((int) Math.pow(3, (ternaryNumberString.length - 1) - i));
}
}
System.out.println(res);
}
private static String balancedTernary(int n)
{
String output = "";
while (n > 0)
{
int rem = n % 3;
n = n / 3;
if (rem == 2)
{
rem = -1;
n++;
}
output = (rem == 0 ? '0' :
(rem == 1) ? '1' : 'Z') + output;
}
return output;
}
}