물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Transform to Prime

BEstyle 2022. 11. 6. 01:07

Task :

Given a List [] of n integers , find minimum number to be inserted in a list, so that sum of all elements of list should equal the closest prime number .


Notes

  • List size is at least 2 .
  • List's numbers will only positives (n > 0) .
  • Repetition of numbers in the list could occur .
  • The newer list's sum should equal the closest prime number .

Input >> Output Examples

1- minimumNumber ({3,1,2}) ==> return (1)

Explanation:

  • Since , the sum of the list's elements equal to (6) , the minimum number to be inserted to transform the sum to prime number is (1) , which will make the sum of the List equal the closest prime number (7) .
2-  minimumNumber ({2,12,8,4,6}) ==> return (5)

Explanation:

  • Since , the sum of the list's elements equal to (32) , the minimum number to be inserted to transform the sum to prime number is (5) , which will make the sum of the List equal the closest prime number (37) .
3- minimumNumber ({50,39,49,6,17,28}) ==> return (2)

Explanation:

  • Since , the sum of the list's elements equal to (189) , the minimum number to be inserted to transform the sum to prime number is (2) , which will make the sum of the List equal the closest prime number (191) .
  •  

def minimum_number(numbers):
    count=0
    a=(sum(numbers))
    while (fprime(a+count)==False):
        count+=1
    return count
    

    
def fprime(n):
    if n<=1:
        return False
    for i in range(2,int(n**(1/2))+1):
        if n%i==0:
            return False
    return True

 

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

Loose Change  (0) 2022.11.07
Triangle Type  (0) 2022.11.07
Autocomplete! Yay!  (0) 2022.11.06
String Average  (0) 2022.11.04
Delete a digit  (0) 2022.11.04