Skip to content

Instantly share code, notes, and snippets.

@jongha
Created May 13, 2019 13:47
Show Gist options
  • Save jongha/807bbb4db51740bad1d4727039cac530 to your computer and use it in GitHub Desktop.
Save jongha/807bbb4db51740bad1d4727039cac530 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <list>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <math.h>
#include <set>
#define FIND 1500
// 2, 3, 5
// 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
int main(void) {
int m[1501];
int pos = 0;
m[pos] = 1;
while(pos < FIND) {
int p2 = 0, p3 = 0, p5 = 0;
while(m[pos] >= m[p2] * 2) ++p2;
while(m[pos] >= m[p3] * 3) ++p3;
while(m[pos] >= m[p5] * 5) ++p5;
++pos;
if(m[pos-1] < m[p2] * 2) m[pos] = m[p2] * 2;
if(m[pos-1] < m[p3] * 3 && m[pos] > m[p3] * 3) m[pos] = m[p3] * 3;
if(m[pos-1] < m[p5] * 5 && m[pos] > m[p5] * 5) m[pos] = m[p5] * 5;
}
/*
for(int i=0; i<FIND; ++i) {
printf("%d\n", m[i]);
}*/
printf("The %d'th ugly number is %d.\n", FIND, m[pos - 1]);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment