- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to check n can be shown as sum of k or not in Python

Suppose we have a number n, and another number k. We have to check whether n can be represented as a sum of k prime numbers or not.

So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.

To solve this, we will follow these steps −

- if n < k*2, then
- return False

- if k > 2, then
- return True

- if k is same as 2, then
- if n is even, then
- return True

- if (n-2) is prime, then
- return True

- return False

- if n is even, then
- if n is prime, then
- return True

- return False

## Example

Let us see the following implementation to get better understanding −

def isPrime(num): if num > 1: for i in range(2, num): if num % i == 0: return False return True return False def solve(n, k): if n < k*2: return False if k > 2: return True if k == 2: if n%2 == 0: return True if isPrime(n-2): return True return False if isPrime(n): return True return False n = 30 k = 3 print(solve(n, k))

## Input

30, 3

## Output

True

- Related Articles
- Program to check n can be represented as sum of k primes or not in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to check we can find three unique elements ose sum is same as k or not Python
- Program to check a number can be written as a sum of distinct factorial numbers or not in Python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Program to check sum of two numbers is up to k from sorted List or not in Python
- Program to check whether we can convert string in K moves or not using Python
- Program to check whether we can get N queens solution or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Program to check whether one point can be converted to another or not in Python
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Program to check words can be found in matrix character board or not in Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check two strings can be equal by swapping characters or not in Python
- Program to check a string can be split into three palindromes or not in Python

Advertisements