给定任何随机范围,我们需要找到Python程序来计算给定范围内具有奇数因子的元素数量。
我不理解问题本身,因此无法尝试给定问题的代码。 帮助我理解问题并找到解决方案。
此代码返回数组中仅包含奇数因子的所有值。
# Import modules
import numpy as np
# -------------- #
# Given a range of values
values = np.arange(0.0, 51.0, 1)
# Function to caclulate factors of a particular number
def get_factors(x):
results = []
for i in range(1, x + 1):
if (x % i == 0):
results.append(i)
return results
# Determine the factor of all values
factors = [get_factors(int(value)) for value in values]
# Function that outputs True if all numbers in given list are odd.
def check_odd(lst):
for num in lst:
if (num % 2 == 0):
return False
return True
# Determine the indices of all elements having odd factors
indices_odd = [i if check_odd(factors[i]) else None for i in range(len(factors))]
indices_odd = np.asarray(indices_odd) # Convert to array
# Determine the number of with odd factos
num_odd_factors = len(np.where(indices_odd != None)[0])
我希望这是你想要的......