import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void basicTest() { assertEquals(4, Kumite.powerOfPrimes(123)); assertEquals(0, Kumite.powerOfPrimes(-1)); assertEquals(0, Kumite.powerOfPrimes(3476)); assertEquals(343, Kumite.powerOfPrimes(344338)); } @Test void onlyOdds() { assertEquals(130321, Kumite.powerOfPrimes(13537)); assertEquals(121, Kumite.powerOfPrimes(731)); assertEquals(-14, Kumite.powerOfPrimes(9311)); assertEquals(-4, Kumite.powerOfPrimes(13)); assertEquals(-50653, Kumite.powerOfPrimes(99757)); assertEquals(1, Kumite.powerOfPrimes(999)); } @Test void onlyEvens() { assertEquals(24, Kumite.powerOfPrimes(24648)); assertEquals(64, Kumite.powerOfPrimes(242)); assertEquals(1, Kumite.powerOfPrimes(888)); assertEquals(1, Kumite.powerOfPrimes(468)); assertEquals(5832, Kumite.powerOfPrimes(24822)); } @Test void onlyNegative() { assertEquals(0, Kumite.powerOfPrimes(-628)); assertEquals(0, Kumite.powerOfPrimes(-25)); } @Test void randomTests() { int max = 1000001; for (int i = 0; i < 1000000; i++) { int num = (int) (Math.random() * max); int expected = Kumite.powerOfPrimes(num); int actual = Kumite.powerOfPrimes(num); assertEquals(expected, actual); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(4, Kumite.powerOfPrimes(123));
- assertEquals(0, Kumite.powerOfPrimes(-1));
- assertEquals(0, Kumite.powerOfPrimes(3476));
- assertEquals(343, Kumite.powerOfPrimes(344338));
- }
- @Test
void onlyOdds(){- void onlyOdds() {
- assertEquals(130321, Kumite.powerOfPrimes(13537));
- assertEquals(121, Kumite.powerOfPrimes(731));
- assertEquals(-14, Kumite.powerOfPrimes(9311));
- assertEquals(-4, Kumite.powerOfPrimes(13));
- assertEquals(-50653, Kumite.powerOfPrimes(99757));
- assertEquals(1, Kumite.powerOfPrimes(999));
- }
- @Test
void onlyEvens(){- void onlyEvens() {
- assertEquals(24, Kumite.powerOfPrimes(24648));
- assertEquals(64, Kumite.powerOfPrimes(242));
- assertEquals(1, Kumite.powerOfPrimes(888));
- assertEquals(1, Kumite.powerOfPrimes(468));
- assertEquals(5832, Kumite.powerOfPrimes(24822));
- }
- @Test
- void onlyNegative() {
- assertEquals(0, Kumite.powerOfPrimes(-628));
- assertEquals(0, Kumite.powerOfPrimes(-25));
- }
- @Test
- void randomTests() {
- int max = 1000001;
- for (int i = 0; i < 1000000; i++) {
- int num = (int) (Math.random() * max);
- int expected = Kumite.powerOfPrimes(num);
- int actual = Kumite.powerOfPrimes(num);
- assertEquals(expected, actual);
- }
- }
- }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { @Test void basicTest() { assertEquals(4, Kumite.powerOfPrimes(123)); assertEquals(1, Kumite.powerOfPrimes(-1)); assertEquals(1, Kumite.powerOfPrimes(666)); assertEquals(1, Kumite.powerOfPrimes(999)); assertEquals(0, Kumite.powerOfPrimes(3476)); assertEquals(343, Kumite.powerOfPrimes(344338)); assertEquals(343, Kumite.powerOfPrimes(344338)); } @Test void randomTests() { int max = 10001; for (int i = 0; i < 1000000; i++) { int num = (int) (Math.random() * max); int expected = Kumite.powerOfPrimes(num); int actual = Kumite.powerOfPrimes(num); assertEquals(expected, actual); } } }
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
- void basicTest() {
- assertEquals(4, Kumite.powerOfPrimes(123));
- assertEquals(1, Kumite.powerOfPrimes(-1));
- assertEquals(1, Kumite.powerOfPrimes(666));
assertEquals(-12, Kumite.powerOfPrimes(-70194));- assertEquals(1, Kumite.powerOfPrimes(999));
- assertEquals(0, Kumite.powerOfPrimes(3476));
- assertEquals(343, Kumite.powerOfPrimes(344338));
- assertEquals(343, Kumite.powerOfPrimes(344338));
- }
- @Test
- void randomTests() {
- int max = 10001;
- for (int i = 0; i < 1000000; i++) {
- int num = (int) (Math.random() * max);
- int expected = Kumite.powerOfPrimes(num);
- int actual = Kumite.powerOfPrimes(num);
- assertEquals(expected, actual);
- }
- }
- }
import java.util.Arrays; public class Kumite { public static int powerOfPrimes(int num) { String numStr = Integer.toString(num); int sumEven = 0; int sumOdd = 0; int exponent = 0; for (int i = 0; i < numStr.length(); i++) { int digit = Character.getNumericValue(numStr.charAt(i)); if(digit % 2 == 0) { sumEven += digit; } else { sumOdd += digit; } int[] primes = {2, 3, 5, 7}; if(Arrays.binarySearch(primes, digit) >= 0) { exponent ++; } } return (int) Math.pow(sumEven - sumOdd, exponent); } }
- import java.util.Arrays;
- public class Kumite {
- public static int powerOfPrimes(int num) {
- String numStr = Integer.toString(num);
- int sumEven = 0;
- int sumOdd = 0;
int base = 0;- int exponent = 0;
- for (int i = 0; i < numStr.length(); i++) {
- int digit = Character.getNumericValue(numStr.charAt(i));
- if(digit % 2 == 0) {
- sumEven += digit;
- } else {
- sumOdd += digit;
- }
- int[] primes = {2, 3, 5, 7};
- if(Arrays.binarySearch(primes, digit) >= 0) {
- exponent ++;
- }
}base = sumEven - sumOdd;- }
return (int) Math.pow(base, exponent);- return (int) Math.pow(sumEven - sumOdd, exponent);
- }
- }
import java.util.Arrays; public class Kumite { public static int powerOfPrimes(int num) { String numStr = Integer.toString(num); int sumEven = 0; int sumOdd = 0; int base = 0; int exponent = 0; for (int i = 0; i < numStr.length(); i++) { int digit = Character.getNumericValue(numStr.charAt(i)); if(digit % 2 == 0) { sumEven += digit; } else { sumOdd += digit; } int[] primes = {2, 3, 5, 7}; if(Arrays.binarySearch(primes, digit) >= 0) { exponent ++; } } base = sumEven - sumOdd; return (int) Math.pow(base, exponent); } }
- import java.util.Arrays;
- public class Kumite {
- public static int powerOfPrimes(int num) {
- String numStr = Integer.toString(num);
- int sumEven = 0;
- int sumOdd = 0;
- int base = 0;
- int exponent = 0;
int result = 0;- for (int i = 0; i < numStr.length(); i++) {
- int digit = Character.getNumericValue(numStr.charAt(i));
- if(digit % 2 == 0) {
- sumEven += digit;
- } else {
- sumOdd += digit;
- }
- int[] primes = {2, 3, 5, 7};
- if(Arrays.binarySearch(primes, digit) >= 0) {
- exponent ++;
- }
- }
- base = sumEven - sumOdd;
result = (int) Math.pow(base, exponent);return result;- return (int) Math.pow(base, exponent);
- }
- }
You must create a function that receives an int number as a parameter.
The sum of the even digits and the sum of the odd digits must be performed.
powerOfPrimes(123) => 4
Then the results of the sums will be subtracted and that result will be raised to the number of prime numbers that has the initial int number.
Note: 1 is not considered prime.
import java.util.Arrays;
public class Kumite {
public static int powerOfPrimes(int num) {
String numStr = Integer.toString(num);
int sumEven = 0;
int sumOdd = 0;
int base = 0;
int exponent = 0;
int result = 0;
for (int i = 0; i < numStr.length(); i++) {
int digit = Character.getNumericValue(numStr.charAt(i));
if(digit % 2 == 0) {
sumEven += digit;
} else {
sumOdd += digit;
}
int[] primes = {2, 3, 5, 7};
if(Arrays.binarySearch(primes, digit) >= 0) {
exponent ++;
}
}
base = sumEven - sumOdd;
result = (int) Math.pow(base, exponent);
return result;
}
}
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SolutionTest {
@Test
void basicTest() {
assertEquals(4, Kumite.powerOfPrimes(123));
assertEquals(1, Kumite.powerOfPrimes(-1));
assertEquals(1, Kumite.powerOfPrimes(666));
assertEquals(1, Kumite.powerOfPrimes(999));
assertEquals(0, Kumite.powerOfPrimes(3476));
assertEquals(343, Kumite.powerOfPrimes(344338));
}
@Test
void randomTests() {
int max = 1000001;
for (int i = 0; i < 1000000; i++) {
int num = (int) (Math.random() * max);
int expected = Kumite.powerOfPrimes(num);
int actual = Kumite.powerOfPrimes(num);
assertEquals(expected, actual);
}
}
}