pine-scriptlcm

Pine script the least common multiple (LCM)


I'm trying to find pine script lcm.

Masters, can you please help? I could not be successful. It should be very simple for you. Please help me

//@version=5



indicator(title='1', overlay=false, precision=2, timeframe='')

x = input(15)

y = input(30)



z =math.max (x,y)



e2=while ((z % x == 0) and (z % y == 0))

lcm:= z

break

z += 1

plot(lcm, color=color.new(color.black, 0))

Solution

  • Your while condition should be an if check inside an infinite while loop.

    This will do:

    //@version=5
    indicator(title='1', overlay=false, precision=2, timeframe='')
    
    x = input(15)
    y = input(30)
    
    z =math.max (x,y)
    lcm = 0
    
    while (1)
        if ((z % x == 0) and (z % y == 0))
            lcm := z
            break
        z += 1
    
    plot(lcm, color=color.new(color.black, 0))