Python-Find the nth Moenissen number __python

Source: Internet
Author: User

Topic Content: Find the nth number of Moenissen. P is prime and M is prime, and satisfies the equation m=2**p-1, then the M is called Moenissen number. For example, p=5,m=2**p-1=31,5 and 31 are primes, so 31 is a Moenissen number.

input Format: Enter with the input () function, noting the return type of this function in Python 3.

output format: int type

Input Sample: 4

Output Sample: 127

Time limit: 500ms memory limit: 32000kb

train of thought: Prime number dozen + enumeration


AC Code:

#-*-Coding:utf-8-*-  
import Math

def prime (n):  # To determine whether the prime number
    if n < 2: return
        False to I in
    rang E (2, int (MATH.SQRT (n)) + 1):
        if n% i = = 0: Return
            False return
    True

def monisen (MAXN):
    "  " # prime number dozen
    Prime_dic = {}
    prime_list = []
    n = 10000
    for I in range (2, n + 1):
        prime_dic[i] = 1
    F or I in range (2, int (n * *. 5) + 1):
        for J in range (I * I, n + 1, i):
            if prime_dic[i] = = 1:
                prime_dic[j] = 0
    for K, v. in Prime_dic.items ():
        If v = 1:
            prime_list.append (k)
    ' ' for
            
    i in prime_list:
  # enumeration 2**prime-1 to MAXN
        Mon = 2 * * I-1
        if Prime (Mon):
            maxn = maxn-1
            if MAXN <= 0: Return
                m On
        
print Monisen (input ())

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.