Wednesday 4 April 2018

Getting super digit of a number



Given an integer 'z' we can find it's super digit as follow:

If it is a single digit number then it itself is a super digit else the super digit is equal to the super digit of the digit-sum of 'z'.

Example:

1) Input: 5
SuperDigit = 5

2) Input: 1234

SuperDigit(1234) = SuperDigit(1+2+3+4)
                             = SuperDigit(10)
                             = SuperDigit(1+0)
                             = SuperDigit(1)
                             = 1

Input Format:
You are given two numbers n and k. You have to calculate the super digit of p. p is created when the number is concatenated k times. That is, if n= 756  and k=2, then p=756756.
That means you have to calculate super digit for 756756.

The first line contains two space-separated integers n and k.

Java Code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

   
    static int digitSum(String n, int k) {
       if(n.length()==1)
           return (Integer.valueOf(n));
        else{
            long sum=0;
            for(int i=0;i<n.length();i++)
            {
                sum=sum+(Long.parseLong(String.valueOf(n.charAt(i))));
               
            }
         
           
            String num=  String.format ("%d", sum);
            return digitSum(num,k);
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String n = in.next();
        int k = in.nextInt();
        long sum=0;
        for(int i=0;i<n.length();i++)
            {
                sum=sum+(Long.parseLong(String.valueOf(n.charAt(i))));
               
            }
        sum=sum*k;
   
        int result = digitSum(Long.toString(sum), k);
        System.out.println(result);
        in.close();
    }
}

Constraints:

1<= n < 10^100000
1<= k< 10^5


Tuesday 23 January 2018

 Printing a Series:

 Consider a series of number generated from the following formula

fn+1=fn-1+(fn-2)^2;

and the first two numbers are a0 = 0 and a1 = 1

Find the nth number of the series?

The logic to solve:

1) Generate the series using the above formula until nth term is obtained
2) Print the nth number in the series

Tips: As n can be a larger number so you need to choose a data type capable of handling the larger number.

Solution (Java Code):

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Demo {

    static BigInteger generateSeries(BigInteger t1, BigInteger t2, BigInteger n) {
        int condition=n.compareTo(new BigInteger("0"));
                if(condition==0)
                    return t2;
                else
                {
                        BigInteger one= new BigInteger("1");
                       
                        return generateSeries(t2,(t1.add(t2.multiply(t2))),n.subtract(one));
                   
                }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       String a = in.next();
        String b = in.next();
        String c = in.next();
        BigInteger t1= new BigInteger(a);
                BigInteger t2= new BigInteger(b);
                BigInteger n= new BigInteger(c);

        BigInteger term= generateSeries(t1, t2, n.subtract(new BigInteger("2")));
        System.out.println(term);
        in.close();
    }

}