Python - n Prime

时间:2018-02-13 04:30:52

标签: python primes

我是python的新手,我正在试图弄清楚如何创建一个输入数字的函数,它会给你数字素数。

示例:如果我输入6则返回13.因为13是第6个素数。

除标准库中的模块外,最好没有模块, 谢谢

2 个答案:

答案 0 :(得分:0)

这就是你要找的东西:

REM sample to adjust credit value (EDIT 02-12-2018 adds history for QB64)
DEFLNG A-Z
credit = 100 ' starting credit
buy = 10 ' amount to add
sell = 10 ' amount to subtract
DO
    COLOR 15
    PRINT "Your credit: "; credit
    COLOR 14
    PRINT " (B)uy for"; buy
    PRINT " (S)ell for"; sell
    PRINT " (H)istory"
    COLOR 15
    PRINT "Enter(Q to quit)? ";
    LOCATE , , 1
    DO
        _LIMIT 100 ' remove for Qbasic
        b$ = INKEY$
        IF LEN(b$) THEN
            PRINT
            EXIT DO
        END IF
    LOOP
    SELECT CASE LCASE$(b$)
        CASE "b"
            credit = credit + sell
            bought = bought + sell
        CASE "s"
            credit = credit - buy
            sold = sold + buy
        CASE "h"
            COLOR 14
            PRINT "Credits bought:"; bought
            PRINT "Credits sold:"; sold
            COLOR 15
            PRINT "Press a key:";
            DO
                _LIMIT 100 ' remove for Qbasic
                x$ = INKEY$
                IF LEN(x$) THEN
                    PRINT
                    EXIT DO
                END IF
            LOOP
        CASE "q"
            COLOR 7
            END
    END SELECT
LOOP
END

答案 1 :(得分:0)

试试这个代码! 我还附上了输出的截图。

num=int(input("Enter Position : "))
r =1000
count=0
import sys
for a in range(2,sys.maxsize**10):
    k=0
    for i in range(2,a//2+1):
        if(a%i==0):
            k=k+1

    if(k<=0):
        count=count+1
    if(count==num):
        print("Prime Number at position " , num , " is " , a)  
        break

enter image description here