Problem url: Download
Description:
let p2=2,p3=3,p5=5
we know ,firt ugly number =1 ;
so ,next ugly number = min(p2*1,p3*1,p5*1)=2
note: 1 represent the first ugly number
next ugly number = min (p2*2,p3*1,p3*1)=3
note: 2 is second U.G & 1 first U.G
next ugly number = min(p2*2,p3*2,p3*1)=min(4,6,5)=4
next ugly number = min(p2*3,p3*2,p3*1)=min(6,6,5)=5
note: 3 is third U.G
***next ugly number =min(p2*3,p3*2,p5*2)=min(6,6,10)[control double 6 by taking next
uglynumber & if necessary loop i decrease, i.e * 6 is not selected in ugly number]
next ugly number= min(p2*4,p3*2,p5*2)=min(8,6,10)=6
note : 4 is fourth ug number
next ugly number= min(p2*4,p3*3,p5*2)=min(8,9,10)=8
next ugly number= min(p2*5,p3*3,p5*2)=min(10,9,10)=9
note : 5 is fifth ug number
next ugly number= min(p2*5,p3*4,p5*2)=min(10,12,10)[above note]
next ugly number= min(p2*6,p3*4,p5*2)=min(12,12,10)=10
note : 6 is sixth ug number
next ugly number= min(p2*6,p3*4,p5*3)=min(12,12,15)[abov note]
next ugly number= min(p2*8,p3*4,p5*3)=min(16,12,15)=12
**** note : 8 is sevrnth ug number
Algorithm:
ug[1]=p2=p3=p5=1;//p2=1,p3=1,p5=1 because the represent the index nub. of ug[]array
For i=2 to 1500 {
if(2*p2 min) then p2++;
else if(3*p3 min) then p3++;
else if(5*p5 min) then p5++;
else if(2*p2==3*p3) then { p2++; i--;}
else if(2*p2==5*p5) then { p2++;i--;}
else if(3*p3==5*p5) then { p3++;i--;}
}
Code
Description:
let p2=2,p3=3,p5=5
we know ,firt ugly number =1 ;
so ,next ugly number = min(p2*1,p3*1,p5*1)=2
note: 1 represent the first ugly number
next ugly number = min (p2*2,p3*1,p3*1)=3
note: 2 is second U.G & 1 first U.G
next ugly number = min(p2*2,p3*2,p3*1)=min(4,6,5)=4
next ugly number = min(p2*3,p3*2,p3*1)=min(6,6,5)=5
note: 3 is third U.G
***next ugly number =min(p2*3,p3*2,p5*2)=min(6,6,10)[control double 6 by taking next
uglynumber & if necessary loop i decrease, i.e * 6 is not selected in ugly number]
next ugly number= min(p2*4,p3*2,p5*2)=min(8,6,10)=6
note : 4 is fourth ug number
next ugly number= min(p2*4,p3*3,p5*2)=min(8,9,10)=8
next ugly number= min(p2*5,p3*3,p5*2)=min(10,9,10)=9
note : 5 is fifth ug number
next ugly number= min(p2*5,p3*4,p5*2)=min(10,12,10)[above note]
next ugly number= min(p2*6,p3*4,p5*2)=min(12,12,10)=10
note : 6 is sixth ug number
next ugly number= min(p2*6,p3*4,p5*3)=min(12,12,15)[abov note]
next ugly number= min(p2*8,p3*4,p5*3)=min(16,12,15)=12
**** note : 8 is sevrnth ug number
Algorithm:
ug[1]=p2=p3=p5=1;//p2=1,p3=1,p5=1 because the represent the index nub. of ug[]array
For i=2 to 1500 {
if(2*p2 min) then p2++;
else if(3*p3 min) then p3++;
else if(5*p5 min) then p5++;
else if(2*p2==3*p3) then { p2++; i--;}
else if(2*p2==5*p5) then { p2++;i--;}
else if(3*p3==5*p5) then { p3++;i--;}
}
Code
No comments:
Post a Comment