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 aa6f4ea

Browse files
committed
Problem 3
1 parent af8016a commit aa6f4ea

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import scala.collection.mutable.ArrayBuffer
2+
3+
object Solution {
4+
5+
val in = {
6+
val lines = scala.io.Source.stdin.getLines
7+
lines flatMap (_ split ' ' filter (_.nonEmpty))
8+
}
9+
10+
def nextInt = in.next.toInt
11+
def nextLong = in.next.toLong
12+
13+
val primes: Stream[Int] = 2 #:: (Stream.from(3, 2) filter (n => primes.view takeWhile (p => p*p <= n) forall (n % _ != 0)))
14+
val primes106 = primes takeWhile (_ < 1000100) toArray
15+
16+
@annotation.tailrec
17+
def largestPrimeFactor(n: Long, index: Int): Long = {
18+
val p: Long = primes106(index)
19+
if (p*p > n) n
20+
else
21+
if (n % p == 0) {
22+
if (n == p) p
23+
else largestPrimeFactor(n/p, index)
24+
} else
25+
largestPrimeFactor(n, index + 1)
26+
}
27+
28+
def main(args: Array[String]): Unit = {
29+
val testCount = nextInt
30+
for (test <- 1 to testCount) {
31+
val N = nextLong
32+
println(largestPrimeFactor(N, 0))
33+
}
34+
}
35+
}

0 commit comments

Comments
(0)

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