Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 039eb4c

Browse files
committed
Problem 10
1 parent dd50900 commit 039eb4c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
object Solution {
2+
3+
val in = {
4+
val lines = scala.io.Source.stdin.getLines
5+
lines flatMap (_ split ' ' filter (_.nonEmpty))
6+
}
7+
8+
def nextInt = in.next.toInt
9+
10+
val primes: Stream[Int] = 2 #:: (Stream.from(3, 2) filter (n => primes.view takeWhile (p => p*p <= n) forall (n % _ != 0)))
11+
12+
def upperBound[T, U](key: U, xs: IndexedSeq[T], from: Int, end: Int, keyExtract: (T) => U = (x:T) => x)(implicit ordering: Ordering[U]): Int = {
13+
var lo = from
14+
var hi = end - 1
15+
while (lo <= hi) {
16+
val mid = (lo + hi) / 2
17+
if (ordering.lt(key, keyExtract(xs(mid))))
18+
hi = mid - 1
19+
else
20+
lo = mid + 1
21+
}
22+
hi + 1
23+
}
24+
25+
def main(args: Array[String]): Unit = {
26+
val p = primes takeWhile (_ < 1000100) toArray
27+
val primeSums = p.scanLeft(0L)(_ + _)
28+
29+
val testCount = nextInt
30+
for (test <- 1 to testCount) {
31+
val N = nextInt
32+
val index = upperBound[Int, Int](N, p, 0, p.size)
33+
println(primeSums(index))
34+
}
35+
}
36+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /