New course! Every coder should learn Generative AI!
Try a free lesson+ 2
[Assignment] Counting numbers not multiples of a0,a1,...,an
You are given M and A=[a0,a1,...,an], all positive integers You program must return the number N of integers between 1 and M not multiple of any of a0...an Ex: M=15 A=[2,3] Ouput : 5 because only 1,5,7,11,13 are not multiples of 2 or 3
15 Answers
+ 2
Sieve is the first thing that comes to mind.
But did anyone try the idea that the number of integers divisible by 2 or 3 is the number of multiples of 2 plus the number of multiples of 3, minus the number of multiples of 6 (lcm of 2 and 3)?
Edit: Code attached. It should be faster for bigger M.
https://code.sololearn.com/cMpGw4uORN6p/?ref=app
+ 2
#python3
M= 15
A = [2, 3]
N = 0
for i in range(1, M+1):
temp = 0
for j in A:
temp += i%j
if not(temp == 0):
print(i, end = ' ')
N += 1
print("N is = {}".format(N) )
+ 2
"Ouput : 5 because only 1,5,7,11,13 are not multiples of 2 or 3"
I feel you meant to say 2 and 3 right? @VcC
if its 2 or 3 then its
1 2 3 4 5 7 8 9 10 11 13 14 15 N is = 13
I edited my previous code.
for eg : (4 is a multiple of 2) or (but not of 3) == true
Kindly clarify
+ 2
VcC
I learned that trick from you. That uber fast numpy sieve.
You forgot to tag me when defining the challange.
+ 2
Kishalaya Saha Good remark. Fast for higher n but not fastest until n=10.000:
https://code.sololearn.com/cQ4Tp2geQaYD/?ref=app
+ 2
VcC Kishalaya Saha
I added Kishalaya's specific example to the list
1000000, [12, 27, 36, 40, 65]
and then his code knocks the socks off the sieve.
https://code.sololearn.com/c2KV9foPRIU8/?ref=app
+ 2
On the other hand, my code can't even handle A=range(2, 30) and M=30 😂
+ 1
Numpy rulez!
https://code.sololearn.com/c10QUf7WfX7g/?ref=app
0
Wrong result. You need a "break" after N+=1
0
After your edit it works. Check here for speed comparisons.
https://code.sololearn.com/copf9UPJ1ec9/?ref=app
0
Louis You win (yours is the last).
https://code.sololearn.com/copf9UPJ1ec9/?ref=app