Adobe Interview Question
Software Engineer / Developersprinting the next 100..
public static void main(String[] args){
int[] ar={1,2,4,5,10,16,20};
LinkedList<Integer> list = new LinkedList();
for(int i:ar){
list.add(i);
}
for(int j=0;j<100;j++){
int next = list.getLast()*2;
for(int i=6;i>=0;i--){
if(next>list.get(i)*2 && list.get(i)*2>list.getLast())
next=list.get(i)*2;
if(next>list.get(i)*5 && list.get(i)*5>list.getLast())
next = list.get(i)*5;
}
System.out.println(next);
list.remove(0);
list.add(next);
}
}
some mistake above..
modified code
public class seq {
public static void main(String[] args){
int[] ar={1,2,4};
LinkedList<Integer> list = new LinkedList();
for(int i:ar){
list.add(i);
}
for(int j=0;j<50;j++){
int next = list.getLast()*2;
for(int i=ar.length-1;i>=0;i--){
if(next>list.get(i)*2 && list.get(i)*2>list.getLast())
next=list.get(i)*2;
if(next>list.get(i)*5 && list.get(i)*5>list.getLast())
next = list.get(i)*5;
}
System.out.println(next);
list.remove(0);
list.add(next);
}
}
}
public class seq {
public static void main(String[] args){
int[] ar={1,2,4};
LinkedList<Integer> list = new LinkedList();
for(int i:ar){
list.add(i);
}
for(int j=0;j<50;j++){
int next = list.getLast()*2;
int prevNext = 0;
for(int i=ar.length-1;i>=0;i--) {
// Checking for 5 first gives the lower next value
if(list.get(i)*5 > list.getLast())
next = list.get(i)*5;
if(list.get(i)*2>list.getLast())
next=list.get(i)*2;
// Get the smaller out of previous next and newer next
if (prevNext > next) {
prevNext = next;
}
}
System.out.println(next);
list.remove(0);
list.add(next);
}
}
}
public class seq {
public static void main(String[] args){
int[] ar={1,2,4};
LinkedList<Integer> list = new LinkedList();
for(int i:ar){
list.add(i);
}
for(int j=0;j<50;j++){
int next = list.getLast()*2;
for(int i=ar.length-1;i>=0;i--){
if(next>list.get(i)*2 && list.get(i)*2>list.getLast())
next=list.get(i)*2;
if(next>list.get(i)*5 && list.get(i)*5>list.getLast())
next = list.get(i)*5;
}
System.out.println(next);
list.remove(0);
list.add(next);
}
}
}
Actually this is called regular number (a slight variation of hamming number)
mathworld.wolfram.com/RegularNumber.html
code for printing first 100 numbers.
import java.util.*;
class Main {
public static void main(String[] args) {
List<Integer> first100 = getHammingNumbers(100);
System.out.println("First 100 Hamming numbers: " + first100);
}
private static void updateFrontier(Integer value,
Set<Integer> frontierSet) {
frontierSet.add(value*2);
frontierSet.add(value*5);
return;
}
public static List<Integer> getHammingNumbers(int howMany) {
List<Integer> completedNumbers = new ArrayList<Integer>();
TreeSet<Integer> frontierSet = new TreeSet<Integer>();
completedNumbers.add(1);
updateFrontier(1, frontierSet);
int count = 1;
while (count < howMany) {
Integer lowestNumber = frontierSet.pollFirst();
completedNumbers.add(lowestNumber);
count++;
updateFrontier(lowestNumber, frontierSet);
}
return completedNumbers;
}
}
output:
First 100 Hamming numbers: [1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 128, 160, 200, 250, 256, 320, 400, 500, 512, 625, 640, 800, 1000, 1024, 1250, 1280, 1600, 2000, 2048, 2500, 2560, 3125, 3200, 4000, 4096, 5000, 5120, 6250, 6400, 8000, 8192, 10000, 10240, 12500, 12800, 15625, 16000, 16384, 20000, 20480, 25000, 25600, 31250, 32000, 32768, 40000, 40960, 50000, 51200, 62500, 64000, 65536, 78125, 80000, 81920, 100000, 102400, 125000, 128000, 131072, 156250, 160000, 163840, 200000, 204800, 250000, 256000, 262144, 312500, 320000, 327680, 390625, 400000, 409600, 500000, 512000, 524288, 625000, 640000, 655360, 781250, 800000, 819200, 1000000]
how come 1 is a multiple of 2 or 5?
- Anonymous March 11, 2011Assuming the begining 1 is by mistake, next number in sequence is 20 ?