问题: 丑陋的数字是唯一的素数因子是2,3或5的数字。序列1,2,3,4,5,6,8,9,10,12,15 ......显示前11个丑陋的数字。按照惯例,包括1。
给定一个数字n
,任务是找到n’th
丑陋的数字。
(https://www.geeksforgeeks.org/ugly-numbers/)
回答:上述链接中的动态编程方法
伪码:
1 Declare an array for ugly numbers: ugly[n]
2 Initialize first ugly no: ugly[0] = 1
3 Initialize three array index variables i2, i3, i5 to point to
1st element of the ugly array:
i2 = i3 = i5 =0;
4 Initialize 3 choices for the next ugly no:
next_mulitple_of_2 = ugly[i2]*2;
next_mulitple_of_3 = ugly[i3]*3
next_mulitple_of_5 = ugly[i5]*5;
5 Now go in a loop to fill all ugly numbers till 150:
For (i = 1; i < 150; i++ )
{
/* These small steps are not optimized for good
readability. Will optimize them in C program */
next_ugly_no = Min(next_mulitple_of_2,
next_mulitple_of_3,
next_mulitple_of_5);
ugly[i] = next_ugly_no
if (next_ugly_no == next_mulitple_of_2)
{
i2 = i2 + 1;
next_mulitple_of_2 = ugly[i2]*2;
}
if (next_ugly_no == next_mulitple_of_3)
{
i3 = i3 + 1;
next_mulitple_of_3 = ugly[i3]*3;
}
if (next_ugly_no == next_mulitple_of_5)
{
i5 = i5 + 1;
next_mulitple_of_5 = ugly[i5]*5;
}
}/* end of for loop */
6.return next_ugly_no
疑惑:
在这种情况下,我不了解DP方法。
为什么我们要跟踪当前丑陋的数字?为什么不经历2,3,5的倍数,每个点保持最小。
喜欢2,3,5。然后4,3,5,然后4,6,5..keeps增加每个的倍数。
相关问题:nth ugly number(我试着通过答案,但我对上面提到的问题感到困惑。)
答案 0 :(得分:2)
进一步优化:通过修剪丑陋的数组以取消设置任何<1的值来实现空间减少。 min(i2,i3,i5)因为它们永远不会再被需要了。
答案 1 :(得分:0)
这是我按照子问题来表达问题的方式:
第i个丑数是通过将另一个丑数(严格小于第i个)乘以2、3或5来获得的。第一个丑数是1。
因此:
// dp[i] ... the i-th ugly number
dp[1] = 1;
i2 = i3 = i5 = 1;
for i in 2 .. n:
dp[i] = min(2*dp[i2], 3*dp[i3], 5*dp[i5]);
// where: 1 <= i2, i3, i5 < i
// update i2, i3, i5 - (this part is tedious)
return dp[n];
答案 2 :(得分:0)
t = int(input())
# function to get all the numbers with prime factors within a given range
def prime(n):
global l
a = []
i = 1
while (i <= n):
k = 0
if (n % i == 0):
j = 1
while (j <= i):
if (i % j == 0):
k = k + 1
j = j + 1
if (k == 2):
a.append(i)
i = i + 1
check(a)
#function to filter all non-ugly numbers
def check(a):
if (all(x in l for x in a)):
everything.append(n)
# main code
while t >0:
h = int(input())
l = [2, 3, 5]
everything = []
for n in range(1, h**2):
prime(n)
print(everything[h - 1])
t-=1