Let M be the least common multiple of all odd integers from 1 to 99, inclusive. What are the last 3 digits of M ?
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.
I don't know Python or any other programming language. So, just did it manually!
Log in to reply
Can you share how to do it manually, preferably in a somewhat smart way?
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.
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
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.
Log in to reply
There's a much more efficient solution. First, find the prime numbers p that are less than or equal to n . Then, find the largest power of p that is less than or equal to n . Multiply those together, then multiply the primes greater than n . Voila.
May I know how did you post solutions with codes?
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
.
1
1
.
1
3
.
1
7
.
1
9
.
2
3
.
2
9
.
3
1
.
3
7
.
4
1
.
4
3
.
4
7
.
5
3
.
5
9
.
6
1
.
6
7
.
7
1
.
7
3
.
7
9
.
8
3
.
8
9
.
9
7
. 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
=
9
9
2
2
5
a
n
d
9
9
2
2
5
≡
2
2
5
(
m
o
d
1
0
0
0
)
;
N
e
x
t
2
2
5
.
1
1
≡
4
7
5
(
m
o
d
1
0
0
0
)
;
4
7
5
.
1
3
≡
1
7
5
(
m
o
d
1
0
0
0
)
;
1
7
5
.
1
7
≡
9
7
5
(
m
o
d
1
0
0
0
)
;
9
7
5
.
1
9
≡
5
2
5
(
m
o
d
1
0
0
0
)
;
5
2
5
.
2
3
≡
7
5
(
m
o
d
1
0
0
0
)
;
7
5
.
2
9
≡
1
7
5
(
m
o
d
1
0
0
0
)
1
7
5
.
3
1
≡
4
2
5
(
m
o
d
1
0
0
0
)
4
2
5
.
3
7
≡
7
2
5
(
m
o
d
1
0
0
0
)
7
2
5
.
4
1
≡
7
2
5
(
m
o
d
1
0
0
0
)
7
2
5
.
4
3
≡
1
7
5
(
m
o
d
1
0
0
0
)
1
7
5
.
4
7
≡
2
2
5
(
m
o
d
1
0
0
0
)
2
2
5
.
5
3
≡
9
2
5
(
m
o
d
1
0
0
0
)
9
2
5
.
5
9
≡
5
7
5
(
m
o
d
1
0
0
0
)
5
7
5
.
6
1
≡
0
7
5
(
m
o
d
1
0
0
0
)
7
5
.
6
7
≡
0
2
5
(
m
o
d
1
0
0
0
)
2
5
.
7
1
≡
7
7
5
(
m
o
d
1
0
0
0
)
7
7
5
.
7
3
≡
5
7
5
(
m
o
d
1
0
0
0
)
5
7
5
.
7
9
≡
4
2
5
(
m
o
d
1
0
0
0
)
4
2
5
.
8
3
≡
2
7
5
(
m
o
d
1
0
0
0
)
2
7
5
.
8
9
≡
4
7
5
(
m
o
d
1
0
0
0
)
4
7
5
.
9
7
≡
0
7
5
(
m
o
d
1
0
0
0
)
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
.
Before introducing the code, we should know that:
lcm ( a , b ) × g cd ( a , b ) = ∣ a b ∣
lcm ( a , b ) = lcm ( a , b ) ∣ a b ∣
and
lcm ( a , b , c ) = lcm ( a , 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 |
|
The code should print out a very large number with the last three digit 0 7 5 .
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
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?
In Haskell:
foldl lcm 1 [3, 5 .. 99]
1 2 3 4 5 6 7 8 9 10 11 |
|
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
Mathematica solution:
FromDigits[IntegerDigits[Apply[LCM, Range[1, 99, 2]]][[-3 ;;]]]
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
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.
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 |
|
Problem Loading...
Note Loading...
Set Loading...
Because of the Associative Laws of l c m :
abs
and
gcd
we can use python's reduce function for constructing a rather short solution.
Note: The last 3 digits are 075, which is why the answer is displayed as 75.