Skip to content

Instantly share code, notes, and snippets.

@nsivabalan
Created December 28, 2013 19:00
Show Gist options
  • Save nsivabalan/8162948 to your computer and use it in GitHub Desktop.
Save nsivabalan/8162948 to your computer and use it in GitHub Desktop.
Lucky Strings : Lucky numbers are those numbers which contain only "4" and/or "5". For example 4, 5, 44, 54,55,444 are lucky numbers while 457, 987 ,154 are not. Lucky number sequence is one in which all lucky numbers exist in increasing order for example 4,5,44,45,54,55,444,445,454,455... Now we concatenate all the lucky numbers (in ascending o…
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class LuckyStrings {
public static void main(String args[]) throws NumberFormatException, IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
if(N == 4)
System.out.println("Hacker");
else
System.out.println("Earth");
}
}
public static int getNthDigit(long n)
{
if(n == 1)
return 4;
if(n == 2)
return 5;
int curDigit = 1;
long totalFoundDigits = 2;
int nextTotalDigits = 0;
while(true){
nextTotalDigits = (int) ((curDigit+1)*Math.pow(2, curDigit+1 ));
if(totalFoundDigits + nextTotalDigits <= n){
totalFoundDigits += nextTotalDigits;
curDigit++;
}
else{
break;
}
}
//System.out.println("totalFound "+totalFoundDigits +" "+n );
return getNumber(curDigit+1,(int) ((n- totalFoundDigits - 1)/(curDigit+1)),(int) (n- totalFoundDigits)%(curDigit+1));
}
private static int getNumber(int noOfDigits, int number, int digit)
{
if(digit == 0) digit = noOfDigits-1 ;
else digit--;
//System.out.println("Params "+noOfDigits+" "+number+" "+digit);
int[] no = new int[noOfDigits];
Arrays.fill(no, 4);
int index = no.length-1;
while(number > 0)
{
int val = number%2;
if(val == 0)
no[index--] = 4;
else
no[index--] = 5;
number /= 2;
}
//System.out.println(Arrays.toString(no));
return no[digit];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment