+ 1

Error in code

The program must add the smallest value to the integer to make it a prime integer optimise your code to avoid time exceeding error. n=int(input()) l=[int(i) for i in input().split()] m,ctr=[],0 for i in l: i=i+ctr for j in range(2,25): if i%j==0: ctr+=1 continue else: m.append(ctr) print(*m) Input: 7 89 54 36 74 44 19 12 Output: 0 5 1 5 3 0 1 My output: 0 5 5 5 6 7 8 Explanation: 89+0 ,54+5,36+1,74+5,44+3,19+0,12+1 gives the smallest prime integer

19th Jun 2020, 6:28 AM
uma
uma - avatar
2 Answers
+ 2
There are some mistakes (explanation after #) n=int(input()) l=[int(i) for i in input().split()] m=[] for i in l: ctr = 0 # Change ctr to 0 every time found = False while found == False: # You should restart cycle for j from 2 for j in range(2, i): # (25 -> i) if i%j==0: ctr+=1 i += 1 # Do not add ctr, because if you add 1, then 2, then 3, actually you add 6 break # Break cycle if it's not an answer. We use "continue" to continue the cycle from the place we are in. For example, if j = 7, when "continue" execute, it continue cycle with j = 8. else: m.append(ctr) found = True print(*m)
19th Jun 2020, 7:19 AM
Maksim
Maksim - avatar