IntStream
Completed
Read n. Using IntStream.rangeClosed, print the sum of 1..n, the sum of squares of the odd numbers, and whether any number in range is divisible by 97 (anyMatch).
Input
One integer.
Output
Three lines.
Example
Input
10
Output
55 165 false
Editor
Java
Hints
IntStream.rangeClosed(1, n) produces 1..n.
asLongStream() avoids int overflow when summing.
Solution explanation
Primitive streams (IntStream, LongStream) avoid boxing and add sum, average, max.
Show solution
import java.util.Scanner;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(IntStream.rangeClosed(1, n).asLongStream().sum());
System.out.println(IntStream.rangeClosed(1, n).filter(x -> x % 2 == 1).asLongStream().map(x -> x * x).sum());
System.out.println(IntStream.rangeClosed(1, n).anyMatch(x -> x % 97 == 0));
}
}