Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.{FileInputStream, File}
- import scala.util.control.Breaks._
- import scala.collection.mutable.ArrayBuffer
- object Main extends App
- {
- val file = new File("K:\\Temp\\primes.txt")
- val primeList = ArrayBuffer[Int]()
- val fis = new FileInputStream(file)
- var str = new StringBuffer
- var q = true
- while (q)
- {
- val dat = Array.fill[Byte](4096)(0)
- val read = fis.read(dat)
- if (read <= 0)
- q = false
- else
- {
- val data = new Array[Byte](read)
- System.arraycopy(dat, 0, data, 0, read)
- str.append(new String(data))
- }
- }
- fis.close
- for (s <- str.toString.drop(1).split(","))
- primeList += s.toInt
- val max = 1000000
- var longest = ArrayBuffer[Int]()
- var lLastPrime = 0
- for (i <- 0 until primeList.length)
- {
- breakable{
- val ab = ArrayBuffer[Int]()
- var lastPrime = 0
- var sum = 0
- for (n <- i until primeList.length)
- {
- sum += primeList(n)
- if (sum < max)
- {
- ab += primeList(n)
- lastPrime = sum
- }
- else
- {
- if (primeList.contains(lastPrime) && ab.length > longest.length)
- {
- longest = ab
- lLastPrime = lastPrime
- }
- break
- }
- }}
- }
- println(s"\nNum: $lLastPrime - ${longest.mkString(", ")}")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement