TheRedRover

Steps in Primes

Apr 27th, 2020
631
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import scala.collection.mutable
  2. object StepInPrimes {
  3.       def cS(end: Int): List[Int] = {
  4.     val primeIndices = mutable.ArrayBuffer.fill((end + 1) / 2)(1)
  5.  
  6.     val intSqrt = Math.sqrt(end).toInt
  7.     for (i <- 3 to end by 2 if i <= intSqrt) {
  8.       for (nonPrime <- i * i to end by 2 * i) {
  9.         primeIndices.update(nonPrime / 2, 0)
  10.       }
  11.     }
  12.  
  13.     (for (i <- primeIndices.indices if primeIndices(i) == 1) yield 2 * i + 1).tail.toList
  14.   }
  15.   def step(g: Int, m: Long, n: Long): String = {
  16.     val filt = cS(n.toInt).filter(_>= m)
  17.     println(filt)
  18. val l =filt.map(_+g).filter(filt.toSet)
  19.     if(l.isEmpty)""
  20.     else {
  21.       val a = l.head
  22.       if (filt.contains(a))
  23.         (a - g, a).toString()
  24.       else ""
  25.     }
  26.   }
  27. }
  28.  
  29.  
  30.  
  31.  
  32. __________________________________
  33.  
  34. object StepInPrimes {
  35.     def step(g: Int, m: Long, n: Long): String = {
  36.         def isPrime(value: Long): Boolean = (2 to Math.round(Math.sqrt(value)).toInt).find(i => value % i == 0).isEmpty
  37.         val result = (m+g to n).find( x => isPrime(x-g) && isPrime(x))
  38.         if (result.isEmpty) "" else (result.get - g, result.get).toString
  39.     }    
  40. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×