How do I calculate LCM?

Dear Team,

I want to find LCM, I try but got error.please help

Hello Tuto

Is your code result wrong or does it trigger an error message? If there is an error message, we need to see it.

Lowest Common Multiple. It looks like you are trying to find the LCM of two numbers - but are those numbers Integers or floats?

Here is a very simple method, limited to calculating the LCM of two Integers.

LCM_2ints.aia (4.4 KB)

2 Likes

You also asked in the Kodular community. It is very disrespectful to ask your question everywhere. Users are spending their free time to help you. Only aak in the community of the builder you use.

3 Likes

Is this the point?

1 Like

I got a recursive solution, and an extension to lists of numbers ...

gcd_test

(I left out validation, for simplicity)

And here is the list based solution ...

lcm_list_test

The procedures are draggable, the tests not draggable.

The .aia and blocks:

lcm.aia (5.0 KB)

P.S. Thank you for a fun question!

2 Likes

This is the oldest and simplest algorithm for gcd:
https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm

1 Like

Here are some suggestions to improve your code, based on a reading of your blocks:

  • Use a value procedure for calculating a result, instead of a global variable. It makes your code simpler, and eliminates worrying about keeping a global variable in sync with a procedure.
  • Make sure that you re-initialize your variables when you start your procedure. Local variables are better for this than global variables.
1 Like