简体   繁体   中英

C++ program to compute lcm of numbers between 1 to 20 (project euler )

as the title explains this is a program to find lcm of numbers between 1 to 20. i found an algorithm to do this, here's the link
http://www.cut-the-knot.org/Curriculum/Arithmetic/LCM.shtml there is a java applet on the webpage that might explain the algorithm better

Problem : i wrote the code compiler shows no error but when i run the code the program goes berserk, i guess may be some infinite loopig but i can't figure it out for the life of me. i use turbo c++ 4.5 so basically if anyone can look at the code and help me out it would be great . thanks in advance

Algorithm :

say we need to find lcm of 2,6,8

first we find the least of the series and add to it the number above it, ie the series become

4,6,8

now we find the least value again and add to it the intitial value in the column ie 2

6,6,8

so the next iteration becomes

8,6,8

8,12,8

10,12,8

10,12,16

12,12,16

14,12,16

14,18,16

16,18,16

18,18,16

18,18,24

20,18,24

20,24,24

22,24,24

24,24,24

as you can see at one point all numbers become equal which is our lcm

#include<iostream.h>
/*function to check if all the elements of an array are equal*/
int equl(int a[20], int n)
{
int i=0;

  while(n==1&&i<20)
 {
  if (a[i]==a[i+1])
        n=1;
  else
        n=0;
        i++;
  }
 return n;
}
/*function to calculate lcm and return that value to main function*/


int lcm()
{
int i,k,j,check=1,a[20],b[20];
/*loading both arrays with numbers from 1 to 20*/
for(i=0;i<20;i++)
{
    a[i]=i+1;
    b[i]=i+1;
}
check= equl(a,1);

/*actual implementation of the algorith*/
while(check==0)
 {
    k=a[0];                  /*looks for the least value in the array*/
    for(i=0;i<20;i++)
    {
        if(a[i+1]<k)
            {
                k=a[i+1];       /*find the least value*/

                j=i+1;          /*mark the position in array */

            }
        else
            continue;
    }
    a[j]=k+b[j];            /*adding the least value with its corresponding number*/

    check= equl(a,1);
 }

 return (a[0]); 

/*at this point all numbers in the array must be same thus any value gives us the lcm*/
}

void main()
{
 int l;
 l=lcm();
 cout<<l;
}

In this line:

a[j]=k+b[j];

You use j but it is unitialized so it's some huge value and you are outside of the array bounds and thus you get a segmentation fault.

You also have some weird things going on in your code. void main() and you use cout without either saying std::cout or using namespace std; or something similar. An odd practice.

Also don't you think you should pass the arrays as arguments if you're going to make lcm() a function? That is int lcm(int a[], int b[]); .

You might look into using a debugger also and improving your coding practices. I found this error within 30 seconds of pasting your code into the compiler with the help of the debugger.

Your loop condition is:

while(n==1&&i<20)

So your equl function will never return 1 because if n happens to be 1 then the loop will just keep going and never return a 1. However, your program still does not appear to return the correct result. You can split the piece of your code that finds the minimum element and replace it with this for cleanliness:

int least(int a[], int size){
    int minPos = 0;

    for(int i=0; i<size ;i++){

        if (a[i] < a[minPos] ){
            minPos = i;
        }
    }

    return minPos;
}

Then you can call it by saying j = least(a, 20); . I will leave further work on your program to you. Consider calling your variables something meaningful instead of i,j,k,a,b .

Your equl function is using array indices from 0-20, but the arrays only have 1-19

j in lcm() is uninitialized if the first element is the smallest. It should be set to 0 at the top of the while loop

In the following code, when i=19, you are accessing a[20] , which is out of the bounds of the array. Should be for(i=0;i<19;i++)

for(i=0;i<20;i++) {
    if(a[i+1]<k)

You are not actually using the std namespace for the cout . this should be std::cout<<l

Your are including iostream.h . The standard is iostream without the .h, this may not work on such an old compiler tho

instead of hard-coding 20 everywhere, you should use a #define . This is not an error, just a style thing.

The following code does nothing. This is the default behavior

else
    continue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM