# Count of ways to represent N as sum of a prime number and twice of a square

Given an integer **N**, the task is to count the number of ways so that N can be written as the sum of a prime number and twice of a square, i.e. , where P can be any prime number and A is any positive integer.

**Note:**

**Examples:**

Input:N = 9

Output:1

Explanation:

9 can be represented as sum of prime number and twice a square in only one way –

Input:N = 15

Output:2

Explanation:

15 can be represented as sum of prime number and twice a square in two ways –

**Approach:** The idea is to use Seive of Eratosthenes to find all the primes and then for each prime number check for every possible number starting from 1. If any prime number and twice a square is equal to the given number then increment the count of the number of ways by 1.

Below is the implementation of the above approach:

## C++

`// C++ implementation to count the ` `// number of ways a number can be ` `// written as sum of prime number ` `// and twice a square ` ` ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` `long` `long` `int` `n = 500000 - 2; ` `vector<` `long` `long` `int` `> v; ` ` ` `// Function to mark all the ` `// prime numbers using sieve ` `void` `sieveoferanthones() ` `{ ` ` ` `bool` `prime[n + 1]; ` ` ` ` ` `// Intially all the numbers ` ` ` `// are marked as prime ` ` ` `memset` `(prime, ` `true` `, ` ` ` `sizeof` `(prime)); ` ` ` ` ` `// Loop to mark the prime numbers ` ` ` `// upto the Square root of N ` ` ` `for` `(` `long` `long` `int` `i = 2; i <= ` `sqrt` `(n); ` ` ` `i++) { ` ` ` `if` `(prime[i]) ` ` ` `for` `(` `long` `long` `int` `j = i * i; ` ` ` `j <= n; j += i) { ` ` ` `prime[j] = ` `false` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Loop to store the prime ` ` ` `// numbers in an array ` ` ` `for` `(` `long` `long` `int` `i = 2; i < n; i++) { ` ` ` `if` `(prime[i]) ` ` ` `v.push_back(i); ` ` ` `} ` `} ` ` ` `// Function to find the number ` `// ways to represent a number ` `// as the sum of prime number and ` `// square of a number ` `void` `numberOfWays(` `long` `long` `int` `n) ` `{ ` ` ` `long` `long` `int` `count = 0; ` ` ` ` ` `// Loop to iterate over all the ` ` ` `// possible prime numbers ` ` ` `for` `(` `long` `long` `int` `j = 1; ` ` ` `2 * (` `pow` `(j, 2)) < n; j++) { ` ` ` `for` `(` `long` `long` `int` `i = 1; ` ` ` `v[i] + 2 <= n; i++) { ` ` ` ` ` `// Incrment the count if ` ` ` `// the given number is a ` ` ` `// valid number ` ` ` `if` `(n == v[i] ` `+ (2 * (` `pow` `(j, 2)))) ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `cout << count << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `sieveoferanthones(); ` ` ` `long` `long` `int` `n = 9; ` ` ` ` ` `// Function Call ` ` ` `numberOfWays(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1

## Recommended Posts:

- Count of ways to split a given number into prime segments
- Number of distinct ways to represent a number as sum of K unique primes
- Represent a number as a sum of maximum possible number of Prime Numbers
- Count of matchsticks required to represent the given number
- Count of integers up to N which represent a Binary number
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Different ways to represent N as sum of K non-zero integers
- Quick ways to check for Prime and find next Prime in Java
- Count number of ways to get Odd Sum
- Count number of ways to divide a number in 4 parts
- Count of square free divisors of a given number
- Count number of ways to arrange first N numbers
- Count ways to express a number as sum of exactly two numbers
- Count number of ways to cover a distance | Set 2
- Count ways to express even number ‘n’ as sum of even integers
- Count of prime digits of a Number which divides the number
- Count the number of ways to construct the target string
- Count number of ways to divide an array into two halves with same sum
- Count the number of ways to divide N in k groups incrementally
- Count ways to express a number as sum of consecutive numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.