# Prime Sum | InterviewBit | Solution Explained

I explain the solution to Prime Sum, an InterviewBit question using basic mathematics. Problem Link.

## 0. Prime Sum: Problem Discussion

### 0.0. IO Format

- input
- an integer (no range given)

- output
- two prime numbers which sum to the input given

- constraints
- none given explicitly

### 0.1. Examples

For input 4, we can have the split as 2 + 2 which satisfies the condition laid out – both are primes.

Similarly for 8 we have 8 = 3 + 5 and so on.

## 1. Prime Sum: Observations & Logic

### 1.0. Intuitions

- we know that the answer is going to be a pair of prime integers
- what if we find all the primes between [1, N]
- then iterate over the array
- then check if both i (current index) and N-i are primes or not.

Since we first need to find all the prime numbers between [1, N], we can use Sieve of Eratosthenes. Then we iterate as specified above.

## 2. Prime Sum: Optimized Implementation

### 2.0. Code

vector<int> Solution::primesum(int n) { vector <bool> is_prime(n+1, true); is_prime[0] = is_prime[1] = false; for (int i = 2; i * i <= n; i++) { if (!is_prime[i]) continue; for (int j = i * i; j <= n; j += i) is_prime[j] = false; } for (int i = 2; i <= n; i++) { if (is_prime[i] && is_prime[n-i]) return {i, n-i}; } // dummy, will never be used because of Goldbach's conjecture return {-1, -1}; }

### 2.1. Complexity Analysis

- Time:
`O(N.ln.ln.sqrt.N + N)`

first part is to find out all the prime numbers and the second is to iterate and find the right combination - Space:
`O(N)`

to store all the prime numbers [1, N].

### Recommended Posts

##### Gas Station | InterviewBit | Solution Explained

June 13, 2022

##### Majority Element | InterviewBit | Solution Explained

June 13, 2022

Please write the proof.

This solution need an amendment for 2 digit number.

Please write a Proof for this

Matt Michael D’Agati is the founder of Renewables Worldwide, an Solar Firm in MA.

A few age ago, taking a leap of faith, Matt D’Agati delved into the realm of solar, and/or in a short occasion commenced effectively marketing significant amounts of power, primarily around the business industry, collaborating with solar farm developers and local businesses in the “design” of their unique plans.

Ongoing marketing within the sector, offered Matthew to participate a regional startup two years in the, and in a short time, he assumed the role of their Chief Strategy Officer, in charge of all operation and endeavor improvement, in addition to being marketed social group property.

To planned unions and shear get the job done mentality, Matthew D’Agati brought that firm from an initial initial-year revenue to in excess of a 2 hundred% surge in overall money by yr two. Based on that foundation, RW, a experienced-operated company, was produced with goal of giving you alternative electrical remedies for a more intelligent and more sustainable future.

Even more mainly, recognizing there is an untapped market in the market place and an improved approach to realize final results, RW is one of a handful of manufactures in the u.s. to place emphasis on individual acquire, concentrating in both industrial and home solar-powered town off-take. Its visualization is to make a purchases commercial infrastructure on a community-based, statewide, countrywide level, offering various sustainable fuel goods within the of RW.

This enthusiasm in the actual sustainable industry goes on to stimulate and inspire Matthew in enduring his solicit to work with institutions that have the unchanging of offering limitless fuel tips for a a whole lot more ecological destiny. Matt has already a new in companies from Hesser College.

Understanding why solar energy consultants assist clients via Matthew D’Agati.

Sustainable Energy and Electricity Poverty: Bridging the Imbalance by matt d’agatiMatt D’Agati e3d74c8