[algorithm] Iterate through the product of two numbers
I need to iterate (from largest to smallest) through the products of two numbers. At first thought this would be as easy as:
Code:
int a = 4;
int b = 4;
int c = 0;
for(; a > 0; a--) {
for(; b > 0; b--) {
c = a * b;
}
b = 4;
}
Forgetting for a moment the problem of repeated products, it won't work. As the code runs you will realize the inner loop will compromise the order. Here's the output of the products (duplicates removed for clarity):
Quote:
16 12 8 4 9 6 3 2 1
I'm not concerned at the issue of repeated products. For my purposes, that won't add any significant weight. I'm however eager to listen to any ideas on how to iterate orderly through the product of two numbers.
Note: You will be correct if you assume I know nothing about the terms of the product. The algorithm can be feed with any kind of terms.