Hammings problem

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1902 Views - Last Post: 11 February 2011 - 05:49 PM Rate Topic: -----

#16 Guest_ahhjan*


Reputation:

Re: Hammings problem

Posted 10 February 2011 - 01:01 PM

View Postmoobler, on 10 February 2011 - 12:35 PM, said:

View Postahhjan, on 10 February 2011 - 02:23 PM, said:

I have another question. At what point would u stop the iterations? AH this is giving me a headache! :@


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


Was This Post Helpful? 0

#17 moobler   User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Hammings problem

Posted 10 February 2011 - 01:38 PM

View Postahhjan, on 10 February 2011 - 03:01 PM, said:

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.


For duplicates, I just make a check before I add any number to the list ('Sequence' in your code). If the number I'm trying to add is equal to last, then I skip it and increase the counter. Otherwise I add the number and update last to equal that number.

I'm still unhappy with my solution for the other part. For each of the multiple counters (count2/3/5 in your code), I keep another counter, which keeps track of the number of times I've increased the multiple counters. Before adding any of the numbers to the sequence, I check to make sure the associated increment counter does not have any prime factors greater than 5. I've tried several methods of figuring this out, but the most efficient seems to be the multiple division of 2, 3, and 5:
private static boolean hasInvalidFactor(int n) {

	while (n % 2 == 0) {
		n /= 2;
	}

	while (n % 3 == 0) {
		n /= 3;
	}

	while (n % 5 == 0) {
		n /= 5;
	}

	// If n is 1, then its only factors are 2, 3, or 5
	return (n != 1);
}


Was This Post Helpful? 2
  • +
  • -

#18 Guest_ahhjan*


Reputation:

Re: Hammings problem

Posted 10 February 2011 - 05:51 PM

View Postmoobler, on 10 February 2011 - 01:38 PM, said:

View Postahhjan, on 10 February 2011 - 03:01 PM, said:

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.


For duplicates, I just make a check before I add any number to the list ('Sequence' in your code). If the number I'm trying to add is equal to last, then I skip it and increase the counter. Otherwise I add the number and update last to equal that number.

I'm still unhappy with my solution for the other part. For each of the multiple counters (count2/3/5 in your code), I keep another counter, which keeps track of the number of times I've increased the multiple counters. Before adding any of the numbers to the sequence, I check to make sure the associated increment counter does not have any prime factors greater than 5. I've tried several methods of figuring this out, but the most efficient seems to be the multiple division of 2, 3, and 5:
private static boolean hasInvalidFactor(int n) {

	while (n % 2 == 0) {
		n /= 2;
	}

	while (n % 3 == 0) {
		n /= 3;
	}

	while (n % 5 == 0) {
		n /= 5;
	}

	// If n is 1, then its only factors are 2, 3, or 5
	return (n != 1);
}



I don't quite get what you're doing there. I am one step away of figuring this out I was able to get rid off all the duplicates now I just need to get rid of those numbers that have other factors. Hmm I am thinking of having a separate method that finds all the prime numbers greater than 5 and put them all in an array then with a for loop check that the numbers I am adding into the sequence aren't divisible by those in the array(other primes). What do you think? I am still not sure if it'll work I am about to try ...
Was This Post Helpful? 0

#19 moobler   User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Hammings problem

Posted 10 February 2011 - 06:03 PM

That will work, but it will also be slow. Go ahead and try it out and once it's working, see if you can refine it.
Was This Post Helpful? 0
  • +
  • -

#20 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Hammings problem

Posted 10 February 2011 - 06:32 PM

@moobler: You could manage those respective counters in a Map, or design a class to encapsualte the base, the count, and a boolean to determine if the number has prime factors > 5. This could help cut down on the repetetive divisions some. :)
Was This Post Helpful? 0
  • +
  • -

#21 Guest_ahhjan*


Reputation:

Re: Hammings problem

Posted 10 February 2011 - 06:48 PM

View Postmoobler, on 10 February 2011 - 06:03 PM, said:

That will work, but it will also be slow. Go ahead and try it out and once it's working, see if you can refine it.


for some reason i can't get my method to work. its giving me some errors, 4 to be exact, saying it is an illegal expressiong? wtf? I dont see anything wrong with it.

	//Method to find primes >5
	public int[] restOfPrimes(){
	   
	   int max = Sequence.get(Sequence.size()-1);
	   
	   for(int i=6; i<=max; i++){
	   	for(int j=6; j<i; j++){
		    int n=i%j;
		    if (n==0)

		          break;		        

		    if(i == j){

		       for(int m=0; m<array.size; m++)
		       primes[m]=i;
		    }  	
		 }
	     }	
	
	   return 
	}//end method



Was This Post Helpful? 0

#22 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Hammings problem

Posted 10 February 2011 - 06:50 PM

Here, you have to return *something* that is an int[] or null. Also, return statements end with a semi-colon as well.
return



In the future, please remember to post your specific errors from your compiler.
Was This Post Helpful? 0
  • +
  • -

#23 Guest_ahhjan*


Reputation:

Re: Hammings problem

Posted 10 February 2011 - 10:30 PM

I am having problems with this part. When I try to test the primes method to see if the prime numbers are in the array it just prints 0's. What am I doing wrong??
Should I try something else? This is getting too complicated. I just don't know how else to get rid of the numbers that are divisible by other primes.

import java.io.*;
import java.util.*;


public class HammingSequence2 {

   //Method to find primes >5
	public static int[] restOfPrimes(int maxValue) {
        
		int [] tempPrimes= new int [maxValue];
		int tempMax= maxValue; 
		  		
	   for(int i=6; i<=tempMax; i++){
	   	for(int j=6; j<i; j++){
		    int n=i%j;
		    if (n==0)
		          break;		        
		    if(i == j){
		       for(int m=0; m<=maxValue; m++){
		       tempPrimes[m]=i;
				 System.out.print(" "+i);
		       }
		    }  	
		  }
	   }	
	
	   return tempPrimes;
   }//end method	
	
   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;
	int maybe=0;

	Sequence.add(1);

	//Find numbers in Hamming Sequence
	while(Sequence.size()<num){
		
		if((count2<=count3) && (count2<=count5)){				
			if(last!=count2){			
			   Sequence.add(count2);
		           last=count2; 
			   count2=count2+2;
			}
			else{
			   count2=count2+2;			
			}
			
		}//end if
		else if((count3<=count2) && (count3<=count5)){			
			if(last!=count3){			
			   Sequence.add(count3);
			   last=count3;
			   count3=count3+3;
			}
			else{
			   count3=count3+3;			
			}
		}//end else if
		else if((count5<=count2) && (count5<=count3)){			
			if(last!=count5){			
			   Sequence.add(count5);
			   last=count5;
			   count5=count5+5;
			}
			else{
			   count5=count5+5;			
			};
		}//end else
				

	}//end while

	int maxValue = Sequence.get(Sequence.size()-1);
	int max=Sequence.size();
	int [] primes = restOfPrimes(maxValue);
  
   //To see what elements are in array. All 0's???
  	for(int i=0; i<primes.length; i++)
		System.out.println(primes[i]);
		
	Collections.sort(Sequence); 
	
	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


Was This Post Helpful? 0

#24 Guest_ahhjan*


Reputation:

Re: Hammings problem

Posted 11 February 2011 - 11:42 AM

View Postmoobler, on 10 February 2011 - 01:38 PM, said:

*snip*
private static boolean hasInvalidFactor(int n) {

	while (n % 2 == 0) {
		n /= 2;
	}

	while (n % 3 == 0) {
		n /= 3;
	}

	while (n % 5 == 0) {
		n /= 5;
	}

	// If n is 1, then its only factors are 2, 3, or 5
	return (n != 1);
}



Hey how do you implement this method. I figured it out but when I try it in my program it seems to go on an infinite loop. Can you help me with this last thing please?? I would really appreciate it. Thanks
import java.io.*;
import java.util.*;


public class HammingSequence2 {

   //Method to find primes >5
   private static boolean hasInvalidFactor(int n){
           
       while(n %2 == 0){
	   n /= 2;
       }
       while(n %3 == 0){
	      n /= 3;
       }    
       while(n %5 == 0){
	      n /= 5;
       }
	   
       return (n!=1);	   
		
   }//end method	
	
   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 trackInv=3;
	int last=0;
	int maybe=0;

	Sequence.add(1);
	
        

	//Find numbers in Hamming Sequence
	while(Sequence.size()<num){
		

		if((count2<=count3) && (count2<=count5)){				
			if((last!=count2)&&(hasInvalidFactor(trackInv)==false))          				{			
			   Sequence.add(count2);
		           last=count2; 
			   trackInv++;
			   count2=count2+2;
			}
			else{
			   count2=count2+2;			
			}
			
		}//end if
		else if((count3<=count2) && (count3<=count5)){			
			if((last!=count3)&&(hasInvalidFactor(trackInv)==false))						 				{			
			   Sequence.add(count3);
	 		   trackInv++;
			   last=count3;
			   count3=count3+3;
			}
			else{
			   count3=count3+3;			
			}
		}//end else if
		else if((count5<=count2) && (count5<=count3)){			
			if((last!=count5)&&(hasInvalidFactor(trackInv)==false)){			
			   Sequence.add(count5);
			   trackInv++;
			   last=count5;
			   count5=count5+5;
			}
			else{
			   count5=count5+5;			
			};
		}//end else
				

	}//end while

		
	Collections.sort(Sequence); 
	
	Integer[] vector= (Integer[])Sequence.toArray(new Integer[0]);
	
	for(Integer temp:vector){
	   int current=temp;
	   System.out.println(" "+temp	); 	
	}


    }//end main


This post has been edited by macosxnerd101: 11 February 2011 - 05:51 PM
Reason for edit:: Removed unnecessary portions of the huge quote

Was This Post Helpful? 0

#25 moobler   User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Hammings problem

Posted 11 February 2011 - 05:49 PM

There are two problems:

The first is that you need to keep separate trackInv values for each counter. The second is that each of these trackInv values needs to be increased every time the associated counter is increased.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2