丑数

丑数

题目描述

把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。


1
2=min(21,31,51)
3=min(2
2,31,51)
4=min(22,32,51)
5=min(2
3,32,51)
6=min(23,32,52)
8=min(2
4,33,52)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function GetUglyNumber_Solution(index)
{
// write code her
const res=[]
if(index<7) return index;//1,2,3,4,5,6都是丑数
res[0]=1;
let two=0,three=0,five=0;
for(var i=1;i<index;i++){
res[i]=Math.min(res[two]*2,res[three]*3,res[five]*5);
if(res[i]===res[two]*2) two++;
if(res[i]===res[three]*3) three++;
if(res[i]===res[five]*5) five++;
}
return res[index-1];
}