diff --git a/others/ugly_numbers.cpp b/others/ugly_numbers.cpp new file mode 100644 index 000000000..227b4aef1 --- /dev/null +++ b/others/ugly_numbers.cpp @@ -0,0 +1,76 @@ +// Ugly numbers are those number whose prime factors are 2, 3 or 5. +// From 1 to 15, there are 11 ugly numbers 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15. +// The numbers 7, 11, 13 are not ugly because they are prime. + +#include +using namespace std; + +int min(int x, int y, int z) { //find smallest among three numbers + if(x < y) { + if(x < z) + return (x); + else + return (z); + }else { + if(y < z) + return (y); + else + return (z); + } +} + +int getUglyNum( long n) { + long uglyNum[n]; // To store ugly numbers + int i2 = 0, i3 = 0, i5 = 0; + + //find next multiple as 1*2, 1*3, 1*5 + + int next2mul = 2; + int next3mul = 3; + int next5mul = 5; + int next = 1; //initially the ugly number is 1 + + uglyNum[0] = 1; + + for (int i=1; i>T; + r=T; + unsigned long arr[T]={}; + while(T!=0) + { + long n=0; + cin>>n; + + arr[T]= (getUglyNum(n)); + T--; + } + for(int i=r;i>0;i--) + { + cout<