Advertisement
PlotnikovPhilipp

Untitled

Sep 20th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. """
  2.  
  3.    Алгоритм определения простого числа
  4.  
  5. """
  6. """
  7. def prime(number):
  8.    if number < 2: return False
  9.    currentNumber = 2
  10.    while currentNumber**2 <= number:
  11.        if number % currentNumber == 0:
  12.            return False
  13.        currentNumber += 1
  14.    return True
  15. print(prime(13)) # вводим любое число
  16. """
  17.  
  18. """
  19.  
  20.    Алгоритм, раскладывающий число n на простые множители
  21.    !!Заметка вернет массив, в котором каждый простой множитель встречается столько раз, какова его степень в разложении n
  22.  
  23. """
  24. """
  25. def factors(number):
  26.    arrayOfSimpleNumbers = []
  27.    currentNumber = 2
  28.    while currentNumber**2 <= number:
  29.        while number % currentNumber == 0:
  30.            arrayOfSimpleNumbers.append(currentNumber)
  31.            number //= currentNumber
  32.        currentNumber += 1
  33.    if number > 1:
  34.        arrayOfSimpleNumbers.append(number)
  35.    return arrayOfSimpleNumbers
  36. print(factors(14))  # вводим любое число
  37. """
  38.  
  39. """
  40.  
  41.    Определения примерного кол-ва простых чисел в диапазоне от 2 до числа n
  42.  
  43. """
  44. """
  45. import math
  46. number = 13 # вводим любое положительное целое число
  47. print(round(number / math.log(number)))
  48. """
  49.  
  50. """
  51.  
  52.    Алгоритм определения точного кол-ва простых множителей числа n
  53.  
  54. """
  55. """
  56. # Используем алгоритм, раскладывающий число n на простые множители
  57. def factors(number):
  58.    arrayOfSimpleNumbers = []
  59.    currentNumber = 2
  60.    while currentNumber**2 <= number:
  61.        while number % currentNumber == 0:
  62.            arrayOfSimpleNumbers.append(currentNumber)
  63.            number //= currentNumber
  64.        currentNumber += 1
  65.    if number > 1:
  66.        arrayOfSimpleNumbers.append(number)
  67.    return arrayOfSimpleNumbers
  68.  
  69. def countOfPrimeFactors(number):
  70.    arrayOfPrimeFactors = factors(number)
  71.    arrayOfUsedNumber = []
  72.    result = 1
  73.    for i in arrayOfPrimeFactors:
  74.        for j in arrayOfUsedNumber:
  75.            if j == i: break
  76.        else:
  77.            result *= (arrayOfPrimeFactors.count(i) + 1)
  78.            arrayOfUsedNumber.append(i)
  79.    return result
  80. print(countOfPrimeFactors(12)) # вводим любой положительное целое число
  81. """
  82.  
  83. """
  84.  
  85.    Решето Эратосфена - алгоритм, эффективно определяющий какие числа являются простыми в диапазоне от 2 до n
  86.  
  87. """
  88. """
  89. def prime(*array):
  90.    number = len(array[0]) - 1
  91.    currentNumber = 2
  92.    while currentNumber <= number:
  93.        if array[0][currentNumber]:
  94.            currentNumber += 1
  95.            continue
  96.        n = currentNumber * 2
  97.        while n <= number:
  98.            array[0][n] = 1
  99.            n += currentNumber
  100.        currentNumber += 1
  101.    return array[0]
  102. array = [0 for i in range(13)] # указываем любое число, где 13, это число n. Индексы списка - числа
  103. print(prime(array))
  104. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement