Questions tagged [lcm]

LCM(a, b) is the smallest positive integer that is divisible by both a and b.

Least common multiple (also called the lowest common multiple or smallest common multiple) of two integers a and b, usually denoted by LCM(a, b), is the smallest positive integer that is divisible by both a and b.

166 questions
2
votes
3 answers

My 'lowest common multiple' program hangs, and doesn't output an answer

I have been trying to get more into programming, so I've been trying to make a simple program that takes two numbers as input, and computes the lowest common multiple. I did this in Python because I don't know how to take input in Java. What happens…
ethanzh
  • 133
  • 2
  • 5
  • 19
1
vote
1 answer

Computing LCM of M consecutive numbers in an array of N integers

I came across this problem here. It was a programming contest held earlier this year. Here is the summary : Given an array of N integers, find LCM of all consecutive M integers. For e.g. Array = [3,5,6,4,8] (hence N = 5) M = 3 Output : …
simplfuzz
  • 12,479
  • 24
  • 84
  • 137
1
vote
3 answers

How to get GCD and LCM of a range of numbers efficiently?

I currently use this code to find gcd and lcm def gcd(a, b): while b != 0: a, b = b, a%b return a def lcm(a, b): result = a*b/gcd(a,b) return result But what if I want to do this for a list of numbers e.g.…
John Smith
  • 11,678
  • 17
  • 46
  • 51
1
vote
0 answers

Recursuve calls in c working even though its not returned to its parent call in stack

I was lookin for a code to implement the LCM of two numbers using recursion. Here is the code i found: #include int lcm(int a, int b) { static int common = 1; if (common % a == 0 && common % b == 0) { return common; …
1
vote
1 answer

How to implement std::lcm to be standard conform?

I'm trying to implement std::lcm and I need to static_assert that the arguments or result are within bounds as required by the standard: The behavior is undefined if |m|, |n|, or the least common multiple of |m| and |n| is not representable as a…
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
1
vote
1 answer

Least common multiple for more than two numbers

I want to find out Least Common Multiple(LCM) of more than two numbers. I know the formula of lcm(a,b) = (a * b) / gcd(a,b). Let's say, I have an array of numbers: [2, 6, 8, 13] and the the lcm should be modulo M = 1000000007. I have seen below code…
Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
1
vote
1 answer

Calculation of Least Common Multiple of the Denominator in Postgresql Database

I want to create a table to better illustrate my question This is a sample of my data in postgresql database The work I want to do is step by step as follows 1-Group the values in the denominator columns by group_id column and calculate the least…
Emre Tuna
  • 105
  • 1
  • 6
1
vote
1 answer

Incorrect value of the variable ans that stores the LCM of two numbers (8086 Program)

Following is the code I wrote to find LCM of two numbers in EMU8086. When I ran it, I am getting value 0 in the Ans variable. .MODEL SMALL .DATA Num1 DW 250 Num2 DW 100 Ans DW ? .CODE MOV AX,@DATA MOV DS, AX MOV AX, Num1 MOV BX, Num2 MOV…
havegudday
  • 65
  • 6
1
vote
1 answer

How to get same result without using Java's reduce method?

I have method which should give the LCM of multiple numbers. It works with Java's reduce() method so the numbers 1,2,3 gives the LCM of 6, which is correct: int lcmAnswer = Arrays.stream(numbers).reduce(1, (a, b) -> { int total = …
superkytoz
  • 1,267
  • 4
  • 23
  • 43
1
vote
2 answers

Output Console freeze after entering the input

I have been trying to code to find the LCM of given Array elements. My code looks like this public long LcmOfArray(List a) { long LCM = 1; bool divisible = false; int divisor = 2, count = 0; while (count != a.Count) { …
Saranya
  • 69
  • 1
  • 4
1
vote
1 answer

Cleanest way to kill Drake simulation from another process

In an example such as examples/allegro_hand, where a main thread advances the simulator and another sends commands to it over LCM, what's the cleanest way for each process to kill the other? I'm struggling to kill the side process when the main…
user12534019
1
vote
3 answers

Find the LCM for up to 5 numbers

I'm trying to write a program that will calculate the LCM of up to five numbers. I've written this code so far: a = b = True while a == True: x = input('\nEnter two to five numbers separated by commas: ').split(',') if x.index(0) == True: …
Kos
  • 83
  • 8
1
vote
4 answers

Java: Program crashes after inputting two bad values

I coded a program, that calculates the gcd (greatest common divisor) and lcm (least common multiple). Everything works fine except the try {...} catch(...) {...}. Here is the part of the code that doesn't work as I want it to: try { num1 =…
Dudek
  • 54
  • 1
  • 7
1
vote
1 answer

Pracma::Lcm() only returns NA in loop

I am trying to obtain the least common multiplier of the prime numbers in between 1 and 20: x <- expand.grid(c(2,3,7,11,13,17,19),c(2,3,7,11,13,17,19)) l <- c() # loop for(i in length(x[,1])){ l[i] <- pracma::Lcm(x[i, 1], x[i, 2]) } #…
1
vote
1 answer

Finding Least common multiple of numbers 1-20, getting the wrong answer

I am trying to solve the Project Euler problem #5, "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" To solve this I created methods to find the LCM (Lowest Common Multiple) using the GCD (Greatest…
BobbyBorn2L8
  • 301
  • 1
  • 2
  • 9
1 2
3
11 12