Skip to content

Instantly share code, notes, and snippets.

@waynejo
Created June 10, 2016 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waynejo/80e4b2b966c473792e3f4380cbd31415 to your computer and use it in GitHub Desktop.
Save waynejo/80e4b2b966c473792e3f4380cbd31415 to your computer and use it in GitHub Desktop.
package Main
import java.io.{FileInputStream, FileOutputStream}
import scala.io.StdIn
object Main extends App {
Console.setIn(new FileInputStream("example.in"))
//
// Console.setIn(new FileInputStream("A-small-practice.in"))
// Console.setOut(new FileOutputStream("A-small-practice.out"))
//
// Console.setIn(new FileInputStream("A-large-practice.in"))
Console.setOut(new FileOutputStream("A-large-practice.out"))
case class Result(answer:String, nds:IndexedSeq[Long])
def getNontrivialDivisor(x:BigInt):Long = {
(2 to Math.sqrt(x.toDouble).floor.toInt).find(0 == x % _).get.toLong
}
def _solve(n:Int, j:Int):Stream[Result] = {
val numbers = Stream.from((1 << (n - 1)) + 1, 2)
numbers.map(_.toBinaryString).filter(x => {
!(2 to 10).exists(y => BigInt(x, y).isProbablePrime(100))
}).take(j).map(x => Result(x,
(2 to 10).map(y => getNontrivialDivisor(BigInt(x, y)))
))
}
def solve():List[Result] = {
val numbers = _solve(16, 500).toList
val result = for (x <- numbers) yield Result(x.answer + x.answer, x.nds)
result.take(500)
}
val cases = StdIn.readLine().toInt
(1 to cases) foreach { i => {
val Array(n, j) = StdIn.readLine().split(" ").map(_.toInt)
println(s"Case #$i:")
val result = solve()
for (r <- result) {
println(s"${r.answer} ${r.nds.mkString(" ")}")
}
}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment