Write a method public static int lowestBasePalindrome int nu

Write a method public static int lowestBasePalindrome (int num) that takes a positive, base-10 integer num as an argument and returns the lowest base greater than or equal to 2 for which the integer num is a palindrome. For example, 100_10 = 10201_3 and 100_10= 1100100_2, so the output of the program would be 3 in this example. In the worst case, the lowest base will be num-1 because a positive integer n written as a base n - 1 number is 11_n - 1. (Consider: if n = 100_10, 100_10 = 11_99 = n_n -1.) Write a method public static int[] interleaveArrays (int [] array 1, int[] array 2, int stride, int maxElements) that interleaves the integers of two arrays, returning the interleaved

Solution

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
public class palindrome {

    static int buildnumber(int i,int n,int s[])//building number from base///
    {
      
        int k=0,m,rm;
        while(n>0)
        {
            m=n%i;

            if(m==0)s[k++]=0;

            while(m>0)
            {
                    s[k++]=m%10;
                    m=m/10;
            }
              
            n=n/i;
        }
     
        return k;
    }
    static int reverse(int n)
    {
        int k=0;
        while(n>0)
        {
            k=k*10+n%10;
            n=n/10;
        }
        return k;
    }
    static boolean palindromecheck(int i,int n)//function that checks palindrome or not
    {
      
        int k,l,j,c=0;
        int s[]=new int[100000000];
        k = buildnumber(i,n,s);
        l=k-1;
        j=0;
        while(j<k)
        {
            if(s[j++]==reverse(s[l--]))c++;
            else break;
        }
      
      
        if(c==k)return true;
      
      
        return false;
    }
    public static int LowestBasePalindrome(int n)//fucntion that returns lowest base integer
    {
            int i;
            for(i=2;i<n;i++)
            {
                if(palindromecheck(i,n)) {
                    return i;
                }
            }
            return i;
    }
    public static void main(String argv[])
    {
        System.out.println(LowestBasePalindrome(917));
    }
  
}

output:-

92

explanation:- for 917 on base 92 the number that generated is 989 which is palindrome

input:-100

output:--

3

 Write a method public static int lowestBasePalindrome (int num) that takes a positive, base-10 integer num as an argument and returns the lowest base greater t
 Write a method public static int lowestBasePalindrome (int num) that takes a positive, base-10 integer num as an argument and returns the lowest base greater t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site