Advertisement
Guest User

Untitled

a guest
Apr 9th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. package coinJam
  2.  
  3. import java.io.File
  4. import java.io.PrintStream
  5. import java.math.BigInteger
  6. import java.util.*
  7. import kotlin.concurrent.timer
  8. import kotlin.system.measureNanoTime
  9. import kotlin.system.measureTimeMillis
  10.  
  11. val out = PrintStream("out.txt")
  12. val random = Random()
  13.  
  14. fun main(args : Array<String>) {
  15. val file = File(args[0]).bufferedReader()
  16. println(measureTimeMillis{
  17. for (i in 1..file.readLine().toInt()) {
  18. val input = file.readLine().split(' ').map { it.toInt() }
  19. solution(i, input[0], input[1])
  20. }
  21. })
  22. }
  23.  
  24. fun solution(case : Int, n : Int, j : Int) {
  25. out.println("Case #$case:")
  26. val composites = mutableListOf<String>()
  27. primeLoop@while (composites.size < j) {
  28. var composite = randomNumber(n)
  29. if (composite in composites) {
  30. continue@primeLoop
  31. }
  32. var factors = mutableListOf<Int>()
  33. radixLoop@for (radix in 2..10) {
  34. var number = BigInteger(composite, radix)
  35. for (i in 2..100) {
  36. if (BigInteger.valueOf((i * i).toLong()) > number) {
  37. continue@primeLoop
  38. }
  39. if (number % BigInteger.valueOf(i.toLong()) == BigInteger.ZERO) {
  40. factors.add(i)
  41. continue@radixLoop
  42. }
  43. }
  44. continue@primeLoop
  45. }
  46. composites.add(composite)
  47. out.println(composite + " " + factors.joinToString(separator = " ") { it.toString() })
  48. }
  49. }
  50.  
  51. fun randomNumber(length : Int): String {
  52. var builder = StringBuilder()
  53.  
  54. builder.append(1)
  55. for (i in 1..(length - 2)) {
  56. builder.append(random.nextInt(2))
  57. }
  58. builder.append(1)
  59.  
  60. return builder.toString()
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement