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
3
votes
2 answers

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 100?

Following is my code where I tried to calculate LCM of all numbers from one to hundred by using BigInteger inn Java. But it does not provide any ans. import java.math.BigInteger; public class CommonOneToHundred { public static void…
kulst
  • 144
  • 1
  • 10
3
votes
2 answers

Computing the approximate LCM of a set of numbers

I'm writing a tone generator program for a microcontroller. I use an hardware timer to trigger an interrupt and check if I need to set the signal to high or low in a particular moment for a given note. I'm using pretty limited hardware, so the…
rrrrrrrrrrrrrrrr
  • 344
  • 5
  • 16
3
votes
2 answers

A more idiomatic way to write this Haskell LCM function

I'm just starting out with Haskell and hammered out this simple recursive algo to find the LCM for every number in a list. It works, but it's messy and I was hoping for some peer-review on how to make this more elegant, readable, and Haskell-y. lcms…
catleeball
  • 781
  • 7
  • 16
3
votes
4 answers

LCM of all the numbers in an array in Java

I have an array of ints, and I'm trying to find the LCM (least common multiple) of all the values in the array. I've written an lcm method separately; it takes two values as input, and returns the lcm. My lcm method works perfectly fine, but when I…
Anindya Guha
  • 150
  • 1
  • 1
  • 8
2
votes
2 answers

C program for finding the lcm of array elements

I'm trying to write a program which updates the smallest number of an array by itself iteratively until all the numbers in the array are the same (an algorithm for finding the lcm). My program seems to end up in an endless loop. My guess is that it…
jvkloc
  • 623
  • 1
  • 5
  • 13
2
votes
2 answers

Trouble Finding Least Common Multiple of Numbers 1-20

"""5. 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to…
2
votes
2 answers

What is the most performant method to calculate the sum of lcm(i, j), i and j ranging from 1 to k?

THE PROBLEM : MY SOLUTION : The solution works, but unfortunately it is really too slow when k is large (k can be a number as big as 69533550916004 for example) var k = 14; var res = 0; for(let i = 1; i <= k; i++){ for(let j = 1; j <= k; j++){ …
2
votes
1 answer

Calculation of Least Common Multiple of the Denominator in Oracle Table

I want to create a table to better illustrate my question This is a sample of my data in oracle 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
2
votes
1 answer

Create a function for Least Common Multiple for a vector in R with helper functions

In R, I have two helper functions gcf(x,y) for finding the greatest common factor of two numbers and lcm(x,y) for finding the lease common multple of two numbers. For example, > gcd(85,75) [1] 5 > lcm(20,50) [1] 100 Now, I need to create a function…
Jayden Rice
  • 301
  • 1
  • 14
2
votes
1 answer

Error finding LCM in javascript with larger numbers

Currently trying to find LCM using js for a course. It seems to work no problem, but when I submit it I am getting a failed result for input 226553150 1023473145 and that is returning 46374212988031340 when it should be returning 46374212988031350…
2
votes
1 answer

FP LCM in scala in 1 line

I am trying to implement LCM in one line in Scala. This is how I've implemented it with 2 functions: def gcd(a: BigInt, b: BigInt):BigInt=if (b==0) a.abs else gcd(b, a%b) def lcm(list: Seq[BigInt]):BigInt=list.foldLeft(BigInt(1))((a, b) =>…
bartosz.lipinski
  • 2,627
  • 2
  • 21
  • 34
2
votes
2 answers

Lowest Common Multiple with doubles in C

I am doing an assignment for a Coursera class that asks me to calculate the Lowest Common Multiple of two numbers, either of which are no larger than 2 * 10 ^ 9. I'm writing this in C and I'm running my code on a test case with the numbers…
foundling
  • 1,695
  • 1
  • 16
  • 22
2
votes
2 answers

Most effecient algorithm for finding this LCM summation

Problem : Find Range of n : 1<= n <= The main challenge is handling queries(Q) which can be large . 1 <= Q <= Methods I have used so far : Brute Force while(Q--) { int N; cin>>N; for(int i=1;i<=N;i++) ans += lcm(i,N)/i…
Sarvagya
  • 89
  • 8
2
votes
6 answers

LCM using recursive?

Here is my code: def lcm(a, b): if b == 0: return a return a * b / lcm(a, b) print lcm(5,3) This is what I could manage so far, any idea on how to find the LCM (least common multiple) of two numbers using recursive and one…
ComputerGuy22
  • 41
  • 1
  • 2
  • 7
2
votes
2 answers

LCM of n numbers modulo 1000000007

I have to find LCM of n numbers MODULO 10^9+7.My approach is find LCM of two numbers and then MOD them.Then take the LCM of next element and the answer obtained from the previous iteration and MOD them.Do this for all elements.Is it wrong ?
user3805465
  • 31
  • 1
  • 3
1
2
3
11 12