Skip to content

Algorithm for Ugly Numbers( DP Solution ) #90

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

Merged
merged 1 commit into from
May 27, 2016
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"max_subarray": "Maximum Subarray",
"max_sum_path": "Maximum Sum Path",
"sliding_window": "Sliding Window",
"integer_partition": "Integer Partition"
"integer_partition": "Integer Partition",
"ugly_numbers": "Ugly Numbers"
}
},
"etc": {
Expand Down
35 changes: 35 additions & 0 deletions algorithm/dp/ugly_numbers/basic/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
for ( var i = 1; i < N; i++ ) {
// next is minimum of m2, m3 and m5
var next = (M[0] <= M[1])?( M[0] <= M[2] )?M[0]:M[2]:( M[1] <= M[2] )?M[1]:M[2];
logger._print( ' Minimum of ' + M[0] + ', ' + M[1] + ', ' + M[2] + " : " + next );
A[i] = next;

tracer._notify( i, A[i] )._wait();
tracer._denotify( i );

if ( next === M[0] ) {
I[0]++;
M[0] = A[I[0]] * 2;
tracer2._notify( 0, M[0])._wait();
tracer3._notify( 0, I[0])._wait();
tracer2._denotify(0);
tracer3._denotify(0);

}
if ( next === M[1] ) {
I[1]++;
M[1] = A[I[1]] * 3;
tracer2._notify( 1, M[1])._wait();
tracer3._notify( 1, I[1])._wait();
tracer2._denotify(1);
tracer3._denotify(1);
}
if ( next === M[2] ) {
I[2]++;
M[2] = A[I[2]] * 5;
tracer2._notify( 2, M[2])._wait();
tracer3._notify( 2, I[2])._wait();
tracer2._denotify(2);
tracer3._denotify(2);
}
}
14 changes: 14 additions & 0 deletions algorithm/dp/ugly_numbers/basic/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var N = 15;
var A = new Array(N);
for (var i = N - 1; i >= 0; i--) {
A[i] = 0;
}
A[0] = 1; // By convention 1 is an ugly number

var M = [2,3,5]; // multiples of 2, 3, 5 respectively
var I = [0,0,0]; // iterators of 2, 3, 5 respectively

var logger = new LogTracer();
var tracer = new Array1DTracer('Ugly Numbers')._setData(A);
var tracer2 = new Array1DTracer('Multiples of 2, 3, 5')._setData(M);
var tracer3 = new Array1DTracer(' Iterators I0, I1, I2 ')._setData(I);
13 changes: 13 additions & 0 deletions algorithm/dp/ugly_numbers/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"Ugly Numbers": "Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence (1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …) shows the first 11 ugly numbers. By convention, 1 is included. The given code displays the first N ugly numbers.",
"Complexity": {
"time": "O(n)",
"space": "O(n)"
},
"References": [
"<a href='http://www.algorithmist.com/index.php/UVa_136'>Algorithmist</a>"
],
"files": {
"basic": "Ugly Numbers"
}
}