Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added program to find nth Ugly Number #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Dynamic Programming/Ugly number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# include<iostream>
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(int n) {
int 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<n; i++) {
next = min(next2mul, next3mul, next5mul); //find next ugly number
uglyNum[i] = next;

if (next == next2mul) {
i2++; //increase iterator of ugly numbers whose factor is 2
next2mul = uglyNum[i2]*2;
}

if (next == next3mul) {
i3++; //increase iterator of ugly numbers whose factor is 3
next3mul = uglyNum[i3]*3;
}

if (next == next5mul) {
i5++; //increase iterator of ugly numbers whose factor is 5
next5mul = uglyNum[i5]*5;
}
}
return next; //the nth ugly number
}

int main() {
int n;
cout << "Enter term: "; cin >> n;
cout << n << "th Ugly number is: " << getUglyNum(n) << endl;
}