moobler, on 10 February 2011 - 12:35 PM, said:
ahhjan, on 10 February 2011 - 02:23 PM, said:
If we tell it how many numbers we want, just keep generating them until the list is full:
public static ArrayList<Integer> getHamming(final int num) {
ArrayList<Integer> hammingNums = new ArrayList<Integer>(num);
while(hammingNums.size() < num) {
// do stuff
}
return hammingNums;
}
Ok. Sorry I ask so many questions. I am having a hard time with this one. But I think i am starting to get it. I am going with your approach cause mine wasn't going to give me all of them. So far this is what I got. And it is working I just need to take care of duplicates. And also the numbers that are divisible by other primes >5.
How did you this? Did you have to check they weren't divisible by others before adding them to the list? or after? You would have to do it before huh because if you did it after then you wouldn't have the n number of elements we want.
import java.io.*;
import java.util.*;
public class HammingSeq2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number n numbers for Hamming Sequence: ");
int num = scan.nextInt();
int counter=0;
ArrayList<Integer> Sequence= new ArrayList<Integer>();
int [] constants = {2,3,5};
int count2=2;
int count3=3;
int count5=5;
int last=0;
Sequence.add(1);
while(Sequence.size()<num){
if((count2<=count3) && (count2<=count5)){
Sequence.add(count2);
count2=count2+2;
}//end if
else if((count3<=count2) && (count3<=count5)){
Sequence.add(count3);
count3=count3+3;
}//end else if
else if((count5<=count2) && (count5<=count3)){
Sequence.add(count5);
count5=count5+5;
}//end else
}//end while
Collections.sort(Sequence);
int max=Sequence.size();
Integer[] vector= (Integer[])Sequence.toArray(new Integer[0]);
int [] seq = new int [max];
for(Integer temp:vector){
int current=temp;
System.out.println(" "+temp );
}
}//end main
}//end class

New Topic/Question
Reply
MultiQuote








|