[Assignment] Counting numbers not multiples of a0,a1,...,an | Sololearn: Learn to code for FREE!
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

10th Aug 2018, 12:49 PM
VcC
VcC - avatar
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
13th Aug 2018, 5:44 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
VcC, Louis Interesting. Doing LCM takes its toll. My complexity depends on the list A and not so much on the number M. So one can try a pretty large M as long as the list A is not too big. Also, my method cannot be modified to produce the actual list of non-multiples :(
13th Aug 2018, 8:15 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 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) )
10th Aug 2018, 1:18 PM
Saithama
+ 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
10th Aug 2018, 2:30 PM
Saithama
+ 2
VcC I learned that trick from you. That uber fast numpy sieve. You forgot to tag me when defining the challange.
12th Aug 2018, 8:53 AM
Louis
Louis - avatar
+ 2
Kishalaya Saha Good remark. Fast for higher n but not fastest until n=10.000: https://code.sololearn.com/cQ4Tp2geQaYD/?ref=app
13th Aug 2018, 7:12 AM
VcC
VcC - avatar
+ 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
13th Aug 2018, 7:54 AM
Louis
Louis - avatar
+ 2
On the other hand, my code can't even handle A=range(2, 30) and M=30 😂
13th Aug 2018, 8:50 AM
Kishalaya Saha
Kishalaya Saha - avatar
10th Aug 2018, 3:09 PM
hinanawi
hinanawi - avatar
12th Aug 2018, 8:42 AM
Louis
Louis - avatar
0
Wrong result. You need a "break" after N+=1
10th Aug 2018, 1:41 PM
VcC
VcC - avatar
0
After your edit it works. Check here for speed comparisons. https://code.sololearn.com/copf9UPJ1ec9/?ref=app
10th Aug 2018, 2:50 PM
VcC
VcC - avatar
12th Aug 2018, 8:49 AM
VcC
VcC - avatar