Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
62
Task/N-smooth-numbers/Nim/n-smooth-numbers.nim
Normal file
62
Task/N-smooth-numbers/Nim/n-smooth-numbers.nim
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import sequtils, strutils
|
||||
import bignum
|
||||
|
||||
const N = 521
|
||||
|
||||
|
||||
func initPrimes(): tuple[primes: seq[Int]; smallPrimes: seq[int]] =
|
||||
|
||||
var sieve: array[2..N, bool]
|
||||
for n in 2..N:
|
||||
if not sieve[n]:
|
||||
for k in countup(n * n, N, n): sieve[k] = true
|
||||
|
||||
for n, isComposite in sieve:
|
||||
if not isComposite:
|
||||
result.primes.add newInt(n)
|
||||
if n <= 29: result.smallPrimes.add n
|
||||
|
||||
# Cache all primes up to N.
|
||||
let (Primes, SmallPrimes) = initPrimes()
|
||||
|
||||
|
||||
|
||||
proc nSmooth(n, size: Positive): seq[Int] =
|
||||
assert n in 2..N, "'n' must be between 2 and " & $N
|
||||
|
||||
let bn = newInt(n)
|
||||
assert bn in Primes, "'n' must be a prime number"
|
||||
|
||||
result.setLen(size)
|
||||
result[0] = newInt(1)
|
||||
|
||||
var next: seq[Int]
|
||||
for prime in Primes:
|
||||
if prime > bn: break
|
||||
next.add prime
|
||||
|
||||
var indices = newSeq[int](next.len)
|
||||
for m in 1..<size:
|
||||
result[m] = next[next.minIndex()]
|
||||
for i in 0..indices.high:
|
||||
if result[m] == next[i]:
|
||||
inc indices[i]
|
||||
next[i] = Primes[i] * result[indices[i]]
|
||||
|
||||
|
||||
when isMainModule:
|
||||
|
||||
for n in SmallPrimes:
|
||||
echo "The first ", n, "-smooth numbers are:"
|
||||
echo nSmooth(n, 25).join(" ")
|
||||
echo ""
|
||||
|
||||
for n in SmallPrimes[1..^1]:
|
||||
echo "The 3000th to 3202th ", n, "-smooth numbers are:"
|
||||
echo nSmooth(n, 3002)[2999..^1].join(" ")
|
||||
echo ""
|
||||
|
||||
for n in [503, 509, 521]:
|
||||
echo "The 30000th to 30019th ", n, "-smooth numbers are:"
|
||||
echo nSmooth(n, 30_019)[29_999..^1].join(" ")
|
||||
echo ""
|
||||
Loading…
Add table
Add a link
Reference in a new issue