How to create the most compact mapping n → isprime(n) up to a limit N?

后端 未结 30 2660
遇见更好的自我
遇见更好的自我 2020-11-22 02:11

Naturally, for bool isprime(number) there would be a data structure I could query.
I define the best algorithm, to be the algorithm that pr

30条回答
  •  隐瞒了意图╮
    2020-11-22 02:52

    You could try something like this.

    def main():
        try:
            user_in = int(input("Enter a number to determine whether the number is prime or not: "))
        except ValueError:
            print()
            print("You must enter a number!")
            print()
            return
        list_range = list(range(2,user_in+1))
        divisor_list = []
        for number in list_range:
            if user_in%number==0:
                divisor_list.append(number)
        if len(divisor_list) < 2:
            print(user_in, "is a prime number!")
            return
        else:
            print(user_in, "is not a prime number!")
            return
    main()
    

提交回复
热议问题