Hamming Numbers

Instructions:

Hamming number is a positive integer of the form 2i3j5k, for some non-negative integers ij, and k.

Write a function that computes the nth smallest Hamming number.

Specifically:

  • The first smallest Hamming number is 1 = 203050
  • The second smallest Hamming number is 2 = 213050
  • The third smallest Hamming number is 3 = 203150
  • The fourth smallest Hamming number is 4 = 223050
  • The fifth smallest Hamming number is 5 = 203051

The 20 smallest Hamming numbers are given in example test fixture.

Your code should be able to compute all of the smallest 5,000 (Clojure: 2000) Hamming numbers without timing out.

Solution:

import java.util.ArrayList;
import java.util.List;

public class Hamming {

	public static long hamming(int n) {
  	// TODO: Program me
        List<Long> result = new ArrayList<>();
        int num2 = 0;
        int num3 = 0;
        int num5 = 0;
        int count = 1;
        result.add(1L);
        while (n > count) {
            long temp = Math.min(Math.min(result.get(num2) * 2, result.get(num3) * 3), result.get(num5) * 5);
            if (temp == result.get(num2) * 2) {
                num2++;
            }
            if (temp == result.get(num3) * 3) {
                num3++;
            }
            if (temp == result.get(num5) * 5) {
                num5++;
            }
            count++;
            result.add(temp);
        }
        return result.get(result.size() - 1);
  }
  
}

Sample Tests:

import org.junit.*;
import static org.junit.Assert.*;

public class HammingTests {

  @Test
  public void Test1() {
    Assert.assertEquals("hamming(1) should be 1", 1, Hamming.hamming(1));
    Assert.assertEquals("hamming(2) should be 2", 2, Hamming.hamming(2));
    Assert.assertEquals("hamming(3) should be 3", 3, Hamming.hamming(3));
    Assert.assertEquals("hamming(4) should be 4", 4, Hamming.hamming(4));
    Assert.assertEquals("hamming(5) should be 5", 5, Hamming.hamming(5));
    Assert.assertEquals("hamming(6) should be 6", 6, Hamming.hamming(6));
    Assert.assertEquals("hamming(7) should be 8", 8, Hamming.hamming(7));
    Assert.assertEquals("hamming(8) should be 9", 9, Hamming.hamming(8));
    Assert.assertEquals("hamming(9) should be 10", 10, Hamming.hamming(9));
    Assert.assertEquals("hamming(10) should be 12", 12, Hamming.hamming(10));
    Assert.assertEquals("hamming(11) should be 15", 15, Hamming.hamming(11));
    Assert.assertEquals("hamming(12) should be 16", 16, Hamming.hamming(12));
    Assert.assertEquals("hamming(13) should be 18", 18, Hamming.hamming(13));
    Assert.assertEquals("hamming(14) should be 20", 20, Hamming.hamming(14));
    Assert.assertEquals("hamming(15) should be 24", 24, Hamming.hamming(15));
    Assert.assertEquals("hamming(16) should be 25", 25, Hamming.hamming(16));
    Assert.assertEquals("hamming(17) should be 27", 27, Hamming.hamming(17));
    Assert.assertEquals("hamming(18) should be 30", 30, Hamming.hamming(18));
    Assert.assertEquals("hamming(19) should be 32", 32, Hamming.hamming(19));
  }

}

猜你喜欢

转载自blog.csdn.net/huangge1199/article/details/106945761