-
Notifications
You must be signed in to change notification settings - Fork 48
Simple Usage of Stream
Hu JiaJun edited this page Nov 29, 2020
·
1 revision
// Return true if an integer is isPrime
boolean isPrime(int n) {
return IntStream
.range(2, n)
.noneMatch(x -> n % x == 0);
}
// Return a stream of integers of all the factors of an integer
IntStream factors(int x) {
return IntStream
.rangeClosed(2, x)
.filter(d -> x % d == 0);
}
// Return a stream of integers of all the prime factors of an integer
IntStream primeFactorsOf(int x) {
return factors(x)
.filter(d -> isPrime(d));
}
// Get the count of all the prime factors of each integer from 1 to n
LongStream omega(int n) {
return IntStream
.rangeClosed(1, n)
.mapToLong(x -> primeFactorsOf(x).count());
}
omega(10).toArray();
/exit
// Use BigInteger class to avoid overflow
// A pair to store 2 elements
class Pair<T, U> {
T first;
U second;
Pair(T first, U second) {
this.first = first;
this.second= second;
}
}
// 0 1
// 1 1
// 1 2
// 2 3
// 3 5
// 5 8
// 8 13
// ...
Stream<BigInteger> fibonacci(int n) {
return Stream.iterate(
new Pair<>(BigInteger.ZERO, BigInteger.ONE), pr -> new Pair<>(pr.second, pr.first.add(pr.second)))
.map(pr -> pr.second).limit(n);
}
fibonacci(5).toArray(BigInteger[]::new);
fibonacci(48).toArray(BigInteger[]::new);
/exit
/**
* n = 3
* k = 2
* 1 * 1 + 1 * 1 = 2
* fibs = 1, 1, 2
*
* n = 4, n = 5
* k = 3
* 1 * 1 + 2 * 1 = 3
* 1 * 1 * 2 * 2 = 5
* fibs = 1, 1, 2, 3, 5
*
* n = 6
* k = 5
* 3 * 1 + 5 * 1 = 8
* 3 * 1 + 5 * 2 = 13
* 3 * 2 + 5 * 3 = 21
* fibs = 1, 1, 2, 3, 5, 8, 13, 21
*
* f5 = f3 + f4 = 1 * f3 + 1 * f4 = f1 * f3 + f2 * f4
* f6 = f4 + f5 = 1 * f3 + 2 * f4 = f2 * f3 + f3 * f4
* f7 = f6 + f7 = 2 * f3 + 3 * f4 = f3 * f3 + f4 * f4
*/
import java.time.Instant;
import java.time.Duration;
List<BigInteger> generateFib(List<BigInteger> fibs) {
List<BigInteger> list = new ArrayList<>(fibs);
int k = list.size();
list.addAll(Stream.
iterate(0, i -> i < k - 1, i -> i + 1).
parallel().
map(i -> fibs.get(k - 2).
multiply(fibs.get(i)).
add(fibs.get(k - 1).
multiply(fibs.get(i+1))
)
).
collect(Collectors.toList()));
return list;
}
BigInteger findFibTerm(int n) {
List<BigInteger> fibList = new ArrayList<>();
fibList.add(BigInteger.ONE);
fibList.add(BigInteger.ONE);
Instant start = Instant.now();
while (fibList.size() < n) {
fibList = generateFib(fibList);
}
Instant stop = Instant.now();
System.out.println(Duration.between(start, stop).toMillis() + "ms");
return fibList.get(n - 1);
}
findFibTerm(50000);
/exit
Peer Learning
Codecrunch Contributions
Piazza Contributions
Wiki Contributions
Guides
Setting Up Checkstyle
Setting Up Java
Setting Up MacVim
Setting Up Sunfire
Setting Up Unix For Mac
Setting Up Unix For Windows
Setting Up Vim
Setting up SSH Config
CS2030 Contents
Lecture 1 SummaryCompile-run vs Run-time Summary
Quick Guide To Abstraction
Generics and Variance of Types
Comparable vs Comparator
Summary of completable future
CS2030S Notes
ELI5 Optional.map vs Optional.flatMap
PECS Example Code
Java Collection Framework (Iterator)
Generic
Generic Type Parameter and Generic Wildcard
Calculator
Lambda-Expression
Single Abstract Method (SAM)
Method Reference
Functional Interfaces 2
Simple Usage of Sandbox
Associative-but-not-commutative
Higher Order function
Functional Programming
Calculator With Functor
Eager Evaluation VS Lazy Evaluation
Simple Usage of Lazy Evaluation
Lazy Evaluation for LazyList
Lazy Evaluation for BinaryTree
Stream
Parallel Stream
Optional
Simple Usage of Stream
Asynchronous Programming
Notes on CompletableFuture
Notes on CompletableFuture 2
Simple Usage of CompletableFuture
Mind Map
Exception Handling
Links
CS2030 Java Style Guide
CS2030 Javadoc Specification
JDK 11 Download Link
JDK 11 API Docs
Codecrunch
Piazza Forum