-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from jabrena/35
35
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/main/java/org/fundamentals/fp/euler/EulerProblem35.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package org.fundamentals.fp.euler; | ||
|
||
import io.reactivex.Single; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import java.util.stream.Stream; | ||
import reactor.core.publisher.Mono; | ||
|
||
/** | ||
* Problem 35: Circular primes | ||
* The number, 197, is called a circular prime because all rotations of the digits: | ||
* 197, 971, and 719, are themselves prime. | ||
* | ||
* There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. | ||
* | ||
* How many circular primes are there below one million? | ||
* | ||
*/ | ||
public class EulerProblem35 implements IEulerType1<Integer, Integer> { | ||
|
||
|
||
@Override | ||
public Integer JavaSolution(Integer limit) { | ||
return null; | ||
} | ||
|
||
Predicate<Integer> isPrime = number -> | ||
(boolean) IntStream.rangeClosed(2, number / 2) | ||
.noneMatch(i -> number % i == 0); | ||
|
||
Function<Integer, List<Long>> toDigits = value -> value.toString().chars() | ||
.mapToObj(c -> String.valueOf((char) c)) | ||
.map(s -> Long.valueOf(s)) | ||
.collect(Collectors.toList()); | ||
|
||
Predicate<Integer> isCircular = number -> { | ||
Stream.of(number) | ||
.map(toDigits) | ||
//.map(ll -> { | ||
//return new CartesianProduct().product(ll); | ||
//}) | ||
.forEach(System.out::println); | ||
return true; | ||
}; | ||
|
||
@Override | ||
public Integer JavaStreamSolution(Integer limit) { | ||
|
||
return (int) IntStream.iterate(limit, i -> i - 1) | ||
.takeWhile(i -> i > 1) | ||
.filter(i -> isPrime.test(i)) | ||
.filter(i -> isCircular.test(i)) | ||
.peek(System.out::println) | ||
.count(); | ||
} | ||
|
||
@Override | ||
public Integer VAVRSolution(Integer limit) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Mono<Integer> ReactorSolution(Integer limit) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Single<Integer> RxJavaSolution(Integer limit) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Integer KotlinSolution(Integer limit) { | ||
return null; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/org/fundamentals/fp/euler/EulerProblem35Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.fundamentals.fp.euler; | ||
|
||
import java.util.List; | ||
import java.util.stream.Stream; | ||
import org.fundamentals.fp.euler.utils.BaseEulerProblemTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
public class EulerProblem35Test extends BaseEulerProblemTest implements IEulerTestable { | ||
|
||
@Override | ||
public void given_JavaSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
} | ||
|
||
@Test | ||
@Override | ||
public void given_JavaStreamSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
EulerProblem35 problem = new EulerProblem35(); | ||
|
||
assertThat(Stream.of("functional programming") | ||
.flatMap(s -> List.of(s.split(" ")).stream()) | ||
.map(s -> s.substring(0,1)) | ||
.map(String::toUpperCase) | ||
.reduce("", (s1, s2) -> s1 + s2)) | ||
.isEqualTo("FP"); | ||
|
||
//assertThat(problem.JavaStreamSolution(100)).isEqualTo(13); | ||
//assertThat(problem.JavaStreamSolution(1000)).isEqualTo(euler.getAnswerToInt(35)); | ||
} | ||
|
||
@Override | ||
public void given_VAVRSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
} | ||
|
||
@Override | ||
public void given_ReactorSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
} | ||
|
||
@Override | ||
public void given_RxJavaSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
} | ||
|
||
@Override | ||
public void given_KotlinSolution_when_executeMethod_then_expectedResultsTest() { | ||
|
||
} | ||
} |