Hamming Numbers

本文介绍了一种计算Hamming数的方法,Hamming数是指形如2^i * 3^j * 5^k的正整数,其中i、j、k是非负整数。文章提供了一个Java实现,能够计算出第n小的Hamming数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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));
  }

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩辕龙儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值