【问题标题】:how to plot prime number in Jupyter notebook如何在 Jupyter 笔记本中绘制素数
【发布时间】:2020-04-30 18:59:43
【问题描述】:

很抱歉再次提醒您有关此问题,我想在 Jupyter 中编写一个程序以在 plot matplot 中显示以下屏幕截图,如何在 jupyter notebook 中编写以下屏幕截图,您的友好回复将不胜感激
the image will describe the graph of the prime number

【问题讨论】:

    标签: python jupyter-notebook


    【解决方案1】:

    首先使用 anaconda 提示符在你的 jupyter notebook 中安装 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】:

      ** 我已经使用 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))
      

      【讨论】:

        猜你喜欢
        • 2021-07-27
        • 2022-07-21
        • 1970-01-01
        • 2018-11-26
        • 2015-10-15
        • 1970-01-01
        • 2017-08-23
        • 1970-01-01
        • 2020-05-17
        相关资源
        最近更新 更多