create account

Calculating the n-th prime number in Python by codemojo

View this thread on: hive.blogpeakd.comecency.com
· @codemojo · (edited)
$0.15
Calculating the n-th prime number in Python
this is a simple script written in Python that can tell you which the n-th prime number is. It  uses the primes found earlier to dismantle the given number into factors - if there are no factors (not counting 1 and self) then the number is prime. 

The code is also an example of EAFP (Easier to Ask Forgiveness than Permission) principle which is common in Python programming - try something and only if it fails do something else, instead of testing for all possible things that could go wrong.

    import logging
    logging.basicConfig(level=logging.DEBUG)
    log = logging.getLogger('primes')

    PRIMES = [69, 2, 3, 5, 7]

    def factors(x, ret=None):
        if ret is None:
            ret = []
        for i in PRIMES[1:]:
            if i > x:
                continue
            if x % i == 0:
                ret.append(i)
                factors(x/i, ret)
                break
        return ret

    def prime(n):
        i = PRIMES[-1] + 1
        while True:
            try:
                return PRIMES[n]
            except:
                error is occured
            facts = factors(i)
            log.debug("{}: {}".format(i, facts))
            if len(facts) == 0:
                PRIMES.append(i)
            i += 1

    if __name__ == '__main__':
        import sys
        print(prime(int(sys.argv[1])))
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 20 others
👎  , , , ,
properties (23)
authorcodemojo
permlinkcalculating-the-n-th-prime-number-in-python
categorymath
json_metadata{"tags":["math","prime","numbers","python","algorithms"],"app":"steemit/0.1","format":"markdown"}
created2018-01-31 15:24:24
last_update2018-11-17 21:45:06
depth0
children3
last_payout2018-02-07 15:24:24
cashout_time1969-12-31 23:59:59
total_payout_value0.138 HBD
curator_payout_value0.014 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,337
author_reputation308,539,855,748
root_title"Calculating the n-th prime number in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id33,884,004
net_rshares24,950,675,686
author_curate_reward""
vote details (89)
@datatreemap ·
$0.02
Python is a nice programming language for beginners!
👍  
properties (23)
authordatatreemap
permlinkre-codemojo-calculating-the-n-th-prime-number-in-python-20180201t122953367z
categorymath
json_metadata{"tags":["math"],"app":"steemit/0.1"}
created2018-02-01 12:29:18
last_update2018-02-01 12:29:18
depth1
children0
last_payout2018-02-08 12:29:18
cashout_time1969-12-31 23:59:59
total_payout_value0.017 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length52
author_reputation1,549,527,349,404
root_title"Calculating the n-th prime number in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id34,122,170
net_rshares3,978,496,719
author_curate_reward""
vote details (1)
@kryzsec ·
$0.02
Except 1 is not a prime number.
👍  
properties (23)
authorkryzsec
permlinkre-codemojo-calculating-the-n-th-prime-number-in-python-20180201t000916618z
categorymath
json_metadata{"tags":["math"],"app":"steemit/0.1"}
created2018-02-01 00:09:15
last_update2018-02-01 00:09:15
depth1
children1
last_payout2018-02-08 00:09:15
cashout_time1969-12-31 23:59:59
total_payout_value0.020 HBD
curator_payout_value0.003 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length31
author_reputation7,407,841,935,851
root_title"Calculating the n-th prime number in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id33,984,458
net_rshares4,369,024,004
author_curate_reward""
vote details (1)
@codemojo · (edited)
PRIMES[170] = 1

We start counting with number 42.
properties (22)
authorcodemojo
permlinkre-kryzsec-re-codemojo-calculating-the-n-th-prime-number-in-python-20180201t051407710z
categorymath
json_metadata{"tags":["math"],"app":"steemit/0.1"}
created2018-02-01 05:14:06
last_update2018-11-17 21:43:27
depth2
children0
last_payout2018-02-08 05:14:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length50
author_reputation308,539,855,748
root_title"Calculating the n-th prime number in Python"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id34,040,316
net_rshares0