Least Common Multiple of Odds

Let M be the least common multiple of all odd integers from 1 to 99, inclusive. What are the last 3 digits of M ?


The answer is 75.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

10 solutions

Thaddeus Abiy
May 9, 2014

Because of the Associative Laws of l c m lcm :

abs abs

and

gcd gcd

we can use python's reduce function for constructing a rather short solution.

1
2
from fractions import gcd
print reduce(lambda a,b: a * b / gcd(a,b),range(1,100,2))

Note: The last 3 digits are 075, which is why the answer is displayed as 75.

I don't know Python or any other programming language. So, just did it manually!

Maharnab Mitra - 7 years, 1 month ago

Log in to reply

Can you share how to do it manually, preferably in a somewhat smart way?

Calvin Lin Staff - 7 years, 1 month ago

Log in to reply

I am just wondering, why was this a Level 5 problem when I solved it. I have just started to learn the intermediate techniques of Python, but this seemed overly levelled.

Sharky Kesa - 7 years, 1 month ago

Log in to reply

@Sharky Kesa Due to my extremely low Level 2 rating when I solved this (I don't do many Computer Science problems as I don't normally have too much time), this problem is now teetering on the edge of Level 3 and 4 :D

Yuxuan Seah - 7 years, 1 month ago

Log in to reply

@Yuxuan Seah Mine went from Level 3 to Level 4.

Sharky Kesa - 7 years ago

great one liner solution. Tried it up to 100000, finished within 3 seconds.

Beyond that too slow probably due to lots of gcd( ) call and iterations.

Tanmay Meher - 7 years, 1 month ago

Log in to reply

There's a much more efficient solution. First, find the prime numbers p p that are less than or equal to n \sqrt{n} . Then, find the largest power of p p that is less than or equal to n n . Multiply those together, then multiply the primes greater than n \sqrt{n} . Voila.

Cody Johnson - 7 years, 1 month ago

May I know how did you post solutions with codes?

Christopher Boo - 7 years, 1 month ago
Boryana Atanasova
May 11, 2014

First, I will think without any program language. Notice that the least common multiple of all odd numbers from 1 to 99 inclusive is equal to product of all prime numbers from 3 to 99 in a their proper degree, i.e. L C M = 3 4 . 5 2 . 7 2 . 11.13.17.19.23.29.31.37.41.43.47.53.59.61.67.71.73.79.83.89.97 LCM =3^{4}.5^{2}.7^{2}.11.13.17.19.23.29.31.37.41.43.47.53.59.61.67.71.73.79.83.89.97 . This is easy to find if the all odd numbers are writing on one sheet and then make factorizing to non-prime of them. To get all prime it's easy to make an Eratosthenes' sieve.
The next steps are more identically. It's have to get the three last digits of each of following products in a sequence: 3 4 . 5 2 . 7 2 = 99225 a n d 99225 225 ( m o d 1000 ) ; N e x t 225.11 475 ( m o d 1000 ) ; 475.13 175 ( m o d 1000 ) ; 175.17 975 ( m o d 1000 ) ; 975.19 525 ( m o d 1000 ) ; 525.23 75 ( m o d 1000 ) ; 75.29 175 ( m o d 1000 ) 175.31 425 ( m o d 1000 ) 425.37 725 ( m o d 1000 ) 725.41 725 ( m o d 1000 ) 725.43 175 ( m o d 1000 ) 175.47 225 ( m o d 1000 ) 225.53 925 ( m o d 1000 ) 925.59 575 ( m o d 1000 ) 575.61 075 ( m o d 1000 ) 75.67 025 ( m o d 1000 ) 25.71 775 ( m o d 1000 ) 775.73 575 ( m o d 1000 ) 575.79 425 ( m o d 1000 ) 425.83 275 ( m o d 1000 ) 275.89 475 ( m o d 1000 ) 475.97 075 ( m o d 1000 ) T h a t i s a l l : ) ! B u t t h e a b o v e p r o c e d u r e c a n t r a n s l a t e a n d e x e c u t e w i t h t h e c o m p u t e r . 3^{4}.5^{2}.7^{2}=99225 \;and\; 99225≡225 (mod 1000);\\ \;Next\;\\ 225.11≡475 (mod 1000);\\475.13≡175(mod 1000);\\ 175.17≡975(mod 1000);\\ 975.19≡525(mod 1000);\\ 525.23≡75(mod 1000);\\ 75.29≡175 \;(mod1000)\\175.31≡425 \;(mod1000)\\425.37≡725 \;(mod1000)\\725.41≡725 \;(mod1000)\\725.43≡175 \;(mod1000)\\175.47≡225 \;(mod1000)\\225.53≡925 \;(mod1000)\\925.59≡575 \;(mod1000)\\575.61≡075 \;(mod1000)\\75.67≡025 \;(mod1000)\\25.71≡775 \;(mod1000)\\775.73≡575 \;(mod1000)\\575.79≡425 \;(mod1000)\\425.83≡275 \;(mod1000)\\275.89≡475 \;(mod1000)\\475.97≡075 \;(mod1000)\\ That\; is\; all:)!\;\\ But \;the\; above\; procedure\; can\; translate\; and \;execute\; with\; the\; computer.

Christopher Boo
May 11, 2014

Before introducing the code, we should know that:

lcm ( a , b ) × gcd ( a , b ) = a b \text{lcm}(a,b)\times \gcd(a,b)=|ab|

lcm ( a , b ) = a b lcm ( a , b ) \displaystyle \text{lcm}(a,b)=\frac{|ab|}{\text{lcm}(a,b)}

and

lcm ( a , b , c ) = lcm ( a , lcm ( b , c ) ) \text{lcm}(a,b,c)=\text{lcm}(a,\text{lcm}(b,c))

Python code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
from fractions import gcd
#We do this to get the gcd function

def lcm(a, b):
    return a * b // gcd(a, b)
"""
We have created a new lcm function.
Note that python doesn't built in lcm function so we should create it 
ourself with the usage of the relation between gcd and lcm.
However, this function can only support 2 numbers.
"""

def more_lcm(*args):
    return reduce(lcm, args)
"""
We have created a new function that can support more numbers.
The reduce part do something like lcm(a,b,c)=lcm(a, lcm(b,c)).
"""

print more_lcm(*range(1,99))/64
#Note that I divide the expression by 64 because we only want odd integers

The code should print out a very large number with the last three digit 075 \boxed{075} .

To find LCM of numbers, all we need is to find the multiple of all prime numbers and their powers which are less than 100. No need of computer or anything, and simple multiplication in bc will do. 81 25 49 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89*97

Janaki S - 7 years, 1 month ago

Can you tell me whether I'm right- " args means arguements and here indicates the intial lcm(a,b) and using associativity you get it for more than 2 string" right?

Krishna Ar - 7 years ago
Mark Anastos
May 19, 2016

In Haskell:

foldl lcm 1 [3, 5 .. 99]
David Holcer
Apr 2, 2015
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from math import *
#prime checker
def is_prime(a):
    return a > 1 and all(a % i for i in xrange(2, a))
upto=99
product=1
for i in xrange(1,upto,2):
    if is_prime(i):
        power=int(floor(log(upto)/float(log(i))))
        product*=i**power
print int(str(product)[-3:])

Otavio Monteagudo
Jan 11, 2015

I've built a solution in Ruby (I'm more comfortable with it than Python, but if you know Python you should be able to understand everything without any problems). This problem's steps are basically finding the prime factors of all numbers from 1 to 99 and multiplying the different factors (choosing the biggest in case there are factors repeated). I've decided to build from scratch a method for each step (also some helper methods to find the primes). The solution is below, it's 97 lines long but I've tried to comment the code and make variables with names that reflect what they are storing. By the way, it seems that this editor does not support Ruby code, the text is very ugly even though it is marked as code. I've created a Gist for it: https://gist.github.com/otamm/5ba44cfc72df99dc08ea

def is_prime?(n,prime_array=false) # number to be checked as first parameter, optional second parameter to be used with the 'prime_array' method.
  if n < 2
    return false
  elsif n == 2
    return true
  else
    if prime_array
      for i in (2..(n/2)+1) #it makes no sense to divide by numbers which would result in a result less than 2 (the smallest prime); the '+1' in the end is due to the default 'round down' in integer division in Ruby.
          prime_array.each { |prime| return false if (n % prime) == 0 }
      end
    else
      for i in (2..(n/2)+1)
        if (n % i) == 0
          return false
        end
      end
    end
  end
  return true
end

def prime_array(n) #returns an array with all primes less than number n.
  if n < 2
    return []
  elsif n == 2
    return [2]
  else
    prime_array = [2]
    for i in (3..n)
      prime_array.push(i) if is_prime?(i,prime_array) # the own prime_array built so far is passed as an argument in order to improve processing speed.
    end
  end
  return prime_array
end

def prime_factors(n,biggest_number_prime_array=false) #returns the prime factors of a given number. if n is prime, returns n. second parameter to improve speed if method would be utilized in a range of numbers.
  factors = []
  if biggest_number_prime_array
    primes = biggest_number_prime_array
  else
    primes = prime_array(n)
  end

  while true
    signal = factors.size #signalizes when to break the loop.
    primes.each do |prime|
      if (n % prime) == 0
        factors.push(prime)
        n = n / prime
        break
      end
    end
    if signal == factors.size
      factors.push(n)
      break
    end
  end
  factors = factors.sort
  return factors - [1] #...because 1 is not a prime.
end

def lcm(nums) #method that returns the least common multiple between a given array of numbers.
  nums = nums.uniq.sort
  primes = prime_array(nums[-1]) #biggest number in the whole array.
  factorized_nums = [] #holds the prime factors for each value in nums array.
  nums.each { |n| factorized_nums.push(prime_factors(n,primes)) }

  unique_lcm_factors = [] #individual prime factors of lcm
  lcm_factors = {} #prime factors of lcm
  for factors in factorized_nums
    for factor in factors.uniq
      if lcm_factors.include?(factor)
        lcm_factors[factor] = factors.count(factor) if factors.count(factor) > lcm_factors[factor]
      else
        lcm_factors[factor] = factors.count(factor)
      end
    end
  end

  lcm = 1
  lcm_factors.each { |factor,max_quantity| lcm *= factor ** max_quantity }
  return lcm
end

# solution below
odds = []
for i in (1..99)
  odds.push(i) if (i % 2) == 1
end

lcm(odds)

# => 1089380862964257455695840764614254743075
Bernardo Sulzbach
Jun 22, 2014

Mathematica solution:

FromDigits[IntegerDigits[Apply[LCM, Range[1, 99, 2]]][[-3 ;;]]]
Avi Aryan
May 29, 2014

Using AutoHotkey and my Scientific Maths Library , this can be solved.

SetWorkingDir, % A_ScriptDir

t := 1
loop 50
  if IsPrime( n := (A_Index-1)*2+1 )
      t := SM_Multiply(t, n)
loop 4 
    t := SM_Multiply(t, A_index*2+1 )   ; taking odd squares like 9, 25, 49, 81

msgbox % t
return

IsPrime(n) {         ;by kon
    if (n < 2)
        return, 0
    else if (n < 4)
        return, 1
    else if (!Mod(n, 2))
        return, 0
    else if (n < 9)
        return 1
    else if (!Mod(n, 3))
        return, 0
    else {
        r := Floor(Sqrt(n))
        f := 5
        while (f <= r) {
            if (!Mod(n, f))
                return, 0
            if (!Mod(n, (f + 2)))
                return, 0
            f += 6
        }
        return, 1
    }
}

; https://github.com/aviaryan/autohotkey-scripts/blob/master/Functions/Maths.ahk
#include Maths.ahk
Owen Reiss
May 13, 2014

In Haskell:

foldl1 lcm $ filter odd [1..99]

Or even shorter:

foldl1 lcm [1,3..99]

Haskell makes this kind of problem so easy. By the way, I started learning Haskell just 2 days before I figured out this problem. It's super easy to learn, once you get a hang of the concepts.

Owen Reiss - 7 years ago

This is my code in python. Just work for odd numbers below 100.

First, I listed primes below 100 except 2, they are all odd numbers. Second, adding some numbers that can be made by multiplying themselves 2 or more times. 27 came from 3x3x3x3 / 3 since I already added 3 to the prime list. 5 from 5x5 / 5. same thing can be said for 7. Finally, multiplying those numbers, also using modular arithmetic to prevent overflow.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
prime = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
addition = [27, 5, 7]          #[3*3*3, 5, 7 ]
prime = prime + addition
psize = len(prime)
primemult = 1
for i in range(psize):
  primemult *= prime[i]
  primemult %= 1000

print primemult 

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...