python - Subtracting the Nearest Prime Given Two Lists using for loop and max -


i have 2 lists: (1) of primes (prime_list), , (2) of odd numbers (odd_list). find highest prime under each odd number, having difficultly. example, odd number 99 want subtract 97.

i have been trying use "for loop" , max. concept can grasp. sure there other method, have been more confused. see example: how find nearest prime number in array, number in array?

           def max_prime():              each_odd in odd_list:                 print(max(prime_list)) 

if add (

how can clean? possible using max , loop?

you can bisect once prime list in sorted order, subtract 1 index returned , prime list:

from bisect import bisect_left  odd_list = range(3, 50, 2) primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71] each_odd in odd_list:     ind = bisect_left(primes, each_odd)     print(each_odd, primes[ind - 1]) 

output:

(3, 2) (5, 3) (7, 5) (9, 7) (11, 7) (13, 11) (15, 13) (17, 13) (19, 17) (21, 19) (23, 19) (25, 23) (27, 23) (29, 23) (31, 29) (33, 31) (35, 31) (37, 31) (39, 37) (41, 37) (43, 41) (45, 43) (47, 43) (49, 47) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -