LoginSignup
0
0

More than 5 years have passed since last update.

シクシク素数列 (Crystal)

Last updated at Posted at 2018-12-20
シクシク素数列 https://qiita.com/advent-calendar/2018/4949prime-series
Crystal https://qiita.com/cielavenir/items/4c95fdf14b7a247e51e0
Kuin https://qiita.com/cielavenir/items/60993a2b2885690a3a4a
Swift https://qiita.com/cielavenir/items/db4c93532a53bfa33ac2
Kotlin https://qiita.com/cielavenir/items/f3ccdd5ebf245aba417c
Pascal https://qiita.com/cielavenir/items/4c962db6825d9a5eb511
4949.cr
#!/usr/bin/env crystal
def isqrt(n)
    return 0 if n<=0
    return 1 if n<4 # 1
    x,y=0,n
    while x!=y&&x+1!=y
        x,y=y,(n/y+y)/2
    end
    x
end

def isPrime(i)
    return false if i<2
    (2..isqrt(i)).each{|j|
        if i%j<1
            return false
        end
    }
    true
end

def is4949(i)
    return false if i<=0
    return true if i%10==4||i%10==9
    is4949(i/10)
end

n=gets.not_nil!.to_i
i=2
cnt=0
while cnt<n
    if is4949(i)&&isPrime(i)
        print i
        if cnt<n-1
            print ","
        else
            puts
        end
        cnt+=1
    end
    i+=1
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0