+0  
 
0
925
4
avatar+234 

Help fix my program. Works but not perfectly; Very long question and in need of explanation of problem; Thanks; <3

Question: 

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

 

Program: 

public class palsquare {
    public static void main(String [] args)throws Exception{
        BufferedReader br = new BufferedReader(new FileReader("palsquare.in"));
        String reader = br.readLine();
        int base = Integer.parseInt(reader);
        PrintWriter pr = new PrintWriter(new FileWriter("palsquare.out"));
        
        
        for(int i = 1; i<300; i++){
            int a  = i * i;
            StringBuilder z = convertBase(base, i);
            StringBuilder b = convertBase(base, a);
            if(isPalindromic(b) == true){
            pr.println(z.toString() + " " + b.toString());
            
            System.out.print(z.toString() + " ");
            System.out.println(b.toString());
        }
            
        }
        pr.close();
    }
    
    public static StringBuilder convertBase(int base,int num){
        int remainder;
        StringBuilder sb = new StringBuilder();
            while(num>0){
                
            int quotient = num / base;
            remainder = num % base;
            sb.insert(0,digit(remainder,base));
            num = quotient;
            }
            
        return sb;
        }
    
    public static String digit(int num, int base){
        String s;
        if(num < (base))
            s = Integer.toString(num);
        else{
            char c = (char) num;
            c+='A';
            c-= 10;
            s = String .valueOf(c);
        }
        return s;
    }
    
    public static boolean isPalindromic(StringBuilder sb){
        boolean isTrue = false;
        String forward = sb.toString();
        String back = sb.reverse().toString();
        if(forward.equals(back))
            isTrue = true;
        return isTrue;
    }

 

Problem: 

input base is 11 and the output should be:

1_1

2_4

3_9

6_33

11_121

22_484

24_565

66_3993

77_5335

101_10201

111_12321

121_14641

202_40804

212_44944

234_53535

Without the underscore.

 

My program outputs the following with base 11:

1_1

2_4

3_9

6_33

11_121

22_484

24_565

66_3993

77_5335

1010_10901

101_10201

111_12321

121_14641

202_40804

212_44944

227_501105

234_53535

The bold ones are the wrong ones. And need to be fixed. Please help.

 Feb 17, 2019
edited by BLANK  Feb 18, 2019
 #1
avatar+368 
+3

What is the program trying to do? You said something about base 11. What about it? Please tell me so I can try to fix it.

 Feb 18, 2019
 #2
avatar+234 
+2

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome.

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on.

Print both the number and its square in base B.

 

basically we test numbers 1-300 ^2 in the base provided. (Square before converting into the base. After that then check to see if the squared number is a palindrome. If it is print out the number that is suppose to be squared in the base form and the squared number in base form if it is palindromic.

BLANK  Feb 18, 2019
edited by BLANK  Feb 18, 2019
 #3
avatar+234 
+1

bro u have still not responded to me.

BLANK  Feb 18, 2019
 #4
avatar+2489 
+3

The Stringbulider sub may have a string concatenation error.  There may be “hidden” data in the file that is read into the string. Generally a read-buffer reads numeric data as numeric and this includes an implied “+” sign which is not visible. This is common to most all programming languages

 

The error that occurs at “101” may also have occurred at “10,” but it didn’t display because it only displays if the number is palindromic. Temporarily set the output to screen-print all converted numbers to see if this is the case.  If it is, then step through the logic to find and correct the length error. 

 

The error at 227 is probably cause by rubbish in the input file. There may be a trailing decimal following the number. This will cause the byte size to jump by 2, because the input read buffer will treat it as a floating point number, instead of an integer.

 

The fix for these errors is usually trivial. It may require stepping through the logic, examining every parameter, checking for string length, and for residual data that may remain in a buffer.   

 

GA

 Feb 19, 2019

2 Online Users