Skip to content

Commit 9b5f439

Browse files
tapankavasthiTapan Avasthi
andauthored
KTLN-692: Check if a number can be expressed as sum of two primes (#758)
Co-authored-by: Tapan Avasthi <[email protected]>
1 parent 51ea7fd commit 9b5f439

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.math.sumofprimes
2+
3+
4+
fun checkPrimeUsingBruteForce(number: Int): Boolean {
5+
if (number <= 1) return false
6+
for (i in 2 until number) {
7+
if (number % i == 0) {
8+
return false
9+
}
10+
}
11+
return true
12+
}
13+
14+
fun canBeExpressedAsSumOfTwoPrimesUsingBruteForceApproach(n: Int): Boolean {
15+
for (i in 2..n / 2) {
16+
if (checkPrimeUsingBruteForce(i) && checkPrimeUsingBruteForce(n - i)) {
17+
return true
18+
}
19+
}
20+
return false
21+
}
22+
23+
fun sieveOfEratosthenes(n: Int): BooleanArray {
24+
val primes = BooleanArray(n + 1) { true }
25+
primes[0] = false
26+
primes[1] = false
27+
for (p in 2..n) {
28+
if (primes[p]) {
29+
for (i in p * p..n step p) {
30+
primes[i] = false
31+
}
32+
}
33+
}
34+
return primes
35+
}
36+
37+
fun canBeExpressedAsSumOfTwoPrimesUsingSieveOfEratosthenes(n: Int): Boolean {
38+
if (n < 2) return false
39+
val primes = sieveOfEratosthenes(n)
40+
for (i in 2 until n) {
41+
if (primes[i] && primes[n - i]) {
42+
return true
43+
}
44+
}
45+
return false
46+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.math.sumofprimes
2+
3+
import kotlin.test.assertTrue
4+
import kotlin.test.assertFalse
5+
import org.junit.jupiter.api.Test
6+
7+
class SumOfTwoPrimesUnitTest {
8+
9+
private val sumOfTwoPrimes = arrayOf(10, 13, 25, 28)
10+
private val notSumOfTwoPrimes = arrayOf(11, 27, 51, 57)
11+
12+
@Test
13+
fun `given number that can be expressed as sum of two primes, when check using brute force approach, then return true`() {
14+
for (n in sumOfTwoPrimes) {
15+
val result = canBeExpressedAsSumOfTwoPrimesUsingBruteForceApproach(n)
16+
assertTrue(result)
17+
}
18+
}
19+
20+
@Test
21+
fun `given number that cannot be expressed as sum of two primes, when check using brute force approach, then return false`() {
22+
for (n in notSumOfTwoPrimes) {
23+
val result = canBeExpressedAsSumOfTwoPrimesUsingBruteForceApproach(n)
24+
assertFalse(result)
25+
}
26+
}
27+
28+
@Test
29+
fun `given number that can be expressed as sum of two primes, when check using sieve of eratosthenes, then return true`() {
30+
for (n in sumOfTwoPrimes) {
31+
val result = canBeExpressedAsSumOfTwoPrimesUsingSieveOfEratosthenes(n)
32+
assertTrue(result)
33+
}
34+
}
35+
36+
@Test
37+
fun `given number that cannot be expressed as sum of two primes, when check using sieve of eratosthenes, then return false`() {
38+
for (n in notSumOfTwoPrimes) {
39+
val result = canBeExpressedAsSumOfTwoPrimesUsingSieveOfEratosthenes(n)
40+
assertFalse(result)
41+
}
42+
}
43+
44+
}

0 commit comments

Comments
 (0)