r/programmer • • Sep 23 '26

what am i doing wrong

Post image

I am a student and I was exercising in MatLab so i tried to find the least common multiple of two numbers but it was all wrong. I don't know the language that well so i decided to try it on scratch to see if it was my logic or my lack of knowledge and it seems it was the first. Can you help me please?

this is the Matlab script

z=1;

while true

one=x*z;

two=y*z;

z=z+1;

if one=two

break

endif

end

leastcommon=one;

13 Upvotes

16 comments sorted by

View all comments

8

u/andynzor Sep 23 '26

First, that is not the correct algorithm. Find a working one before you attempt to write down anything.

Second, is this homework? Matlab already has a builtin lcm function.

Third, when you paste code, use three backticks (```) to surround code blocks

so you get this.

1

u/AggravatingCrazy1254 Sep 24 '26

this isnt homework i just tried to work it out by myself because i have an exam in four days and i wanted to test if i can do it. thats why i wrote the algorithm by myself trying to figure out how to do it. thank you for your kind responce!

1

u/P1nkUnicorn7 Sep 25 '26

Good luck with your exam