如何在Jupyter笔记本中绘制素数

2024-04-19 20:23:07 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉再次提醒您这个问题,我想在Jupyter中编写一个程序,在plot matplot中显示下面的屏幕截图,如何在Jupyter笔记本中绘制下面的屏幕截图您的善意回复将不胜感激
the image will describe the graph of the prime number


Tags: oftheimage程序number屏幕plot绘制
1条回答
网友
1楼 · 发布于 2024-04-19 20:23:07

首先,通过键入以下命令,使用anaconda提示符在jupyter笔记本中安装matplotlib

pip install matplotlib

在使用编程逻辑在列表或数组中生成素数之后,您可能会找到生成素数的程序,在生成素数之后,请尝试这行代码

# importing the required module 
import matplotlib.pyplot as plt 

# x axis values of prime numbers 
x = [1,2,3] 
# corresponding y axis values of prime numbers 
y = [5,7,11] 

# plotting the points  
plt.plot(x, y) 

# naming the x axis 
plt.xlabel('x - axis') 
# naming the y axis 
plt.ylabel('y - axis') 

# giving a title to my graph 
plt.title('My first graph!') 

# function to show the plot 
plt.show()

我给你一个开始,你可以生成任何你想要的范围,然后相应地绘图。 如果你需要更多的帮助,请尽管问,我会看看我能为你做些什么

网友
2楼 · 发布于 2024-04-19 20:23:07

**我已使用Miller-Rabin测试编写了以下生成素数的程序,如何在生成素数后将您的程序附加到此程序,我可以同时在图表中显示结果或输出,我们将不胜感激。**

import random
import time
# Utility function to do
# modular exponentiation.
# It returns (x^y) % p
def power(x, y, p):
    # Initialize result
    res = 1;

    # Update x if it is more than or
    # equal to p
    x = x % p;
    while (y > 0):

        # If y is odd, multiply
        # x with result
        if (y & 1):
            res = (res * x) % p;

        # y must be even now
        y = y >> 1;  # y = y/2
        x = (x * x) % p;

    return res;

# This function is called
# for all k trials. It returns
# false if n is composite and
# returns false if n is
# probably prime. d is an odd
# number such that d*2<sup>r</sup> = n-1
# for some r >= 1
def miillerTest(d, n):

    # Pick a random number in [2..n-2]
    # Corner cases make sure that n > 4
    a = 2 + random.randint(1, n - 4);

    # Compute a^d % n
    x = power(a, d, n);

    if (x == 1 or x == n - 1):
        return True;
    # Keep squaring x while one
    # of the following doesn't
    # happen
    # (i) d does not reach n-1
    # (ii) (x^2) % n is not 1
    # (iii) (x^2) % n is not n-1
    while (d != n - 1):
        x = (x * x) % n;
        d *= 2;

        if (x == 1):
            return False;
        if (x == n - 1):
            return True;

        # Return composite
    return False;

# It returns false if n is
# composite and returns true if n
# is probably prime. k is an
# input parameter that determines
# accuracy level. Higher value of
# k indicates more accuracy.
def isPrime(n, k):
    p = 2
    # Corner cases
    if (n <= 1 or n == 4):
        return False;
    if (n <= 3):
        return True;

    # Find r such that n =
    # 2^d * r + 1 for some r >= 1
    d = n - 1;
    while (d % 2 == 0):
        d //= 2;

    # Iterate given nber of 'k' times

    for i in range(k):
        if (miillerTest(d, n) == False):
            return False;
    return True;
# Driver Code
# Number of iterations
k = 7;
start_time=time.time()

print("All primes between 10^6and 10^111: ");

for n in range(10, 1000):
    if (isPrime(n, k)):
        print(n, end=" ");
    end_time = time.time()

print("Total time:%0.5f" % (end_time - start_time))

相关问题 更多 >