Improvements Required

I have made a Common Multiplier (you enter 2 numbers and the program will find the lowest common multiple). I have tried it out and it works perfectly fine but I want to ask for some advice to improve this area.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;


int main()
{
    restart:
    int number1; 
    int number2;
    long int i;
    char letter;
    i = 1;
    cout << "Hello. Please enter your first number." << endl;
    cin >> number1;
    cout << "Now, please enter your second number." << endl;
    cin >> number2;
    cout << "Thank you. Please allow the machine to process \nthe number's lowest common multiple." << endl;
    while (i % number1 > 0 | i % number2 > 0) 
    {    
         i++;
    }
    cout << "The lowest common multiple of " << number1 << " and " << number2 << " is " << i << "." << endl;
     
    cout << "Type 'Y' or 'y' to run again or press anything else to exit" << endl;
    cin >> letter;
    while (letter == 'Y' || letter == 'y')
       { 
               system("CLS");
               goto restart;
       }
               system("PAUSE");
               return(0);
}         


That is my code. Because my code works like 'I = 1' and keeps adding 1 to it, it processes pretty slow for high integers. Does anyone know how to speed up that bit so it can process higher numbers faster?

Thanks ^^
Use the logical OR in the while. It'll save you a few comparisons.
Okay
Like this?
while (i % number1 > 0 or i % number2 > 0)
Last edited on
No, not like that.
THIS is the logical OR operator: ||
Oh okay. Thanks, I'll try that. May I ask what is the difference between | and ||?
Last edited on
See http://en.wikipedia.org/wiki/Lowest_common_multiple

The formula for LCD( ab ) = ab / GCD( ab )

where GCD is the greatest common divisor. There are well known algorithms to compute
the GCD quickly without having to loop through all integers.

| = bitwise OR
|| = logical (boolean) OR

LCM, not LCD, jsmith.
Topic archived. No new replies allowed.