I'm trying to write in python3 to determine whether a number is a prime number or not.
I was specifically demanded to only use the following method:
"Divide the input with all the positive prime number smaller than it's square root."
For example, if the given number is 33, and then I would have to divide 33 with [2,3,5] (smaller than 5.xx, the square root of 33)
Meanwhile, in the process of finding [2,3,5], I can not use any method other than the demanded one.
So my code are as follow:
def is_prime(num):
import math
a=math.sqrt(num)
llist=[2,3]
pri=0
for i in range(2,int(a)+1):
root=math.sqrt(i)
for m in llist:
if m<root:
left=i%m
if left!=0:
llist.append(i)
if num in llist:
return True
for m in llist:
if num%m==0:
return False
if num%m!=0:
pri=pri+1
if pri==len(llist):
return True
and the code can not properly run when the input number exceed 7 figures, it just stop responding.
Apparently somewhere in my code there is an infinite loop that I can't figure out.
I'd be very grateful if someone can help me out with this one.
Related
Why is why is np.exp(x) not equal to np.exp(1)**x?
For example:
np.exp(400)
>>>5.221469689764144e+173
np.exp(1)**400
>>>5.221469689764033e+173
np.exp(400)-np.exp(1)**400
>>>1.1093513018771065e+160
This is optimisation of numpy that raise this diff.
Indeed, you have to understand how is calculated the Euler number in math:
e = (1/n)**n with n == inf.
I think numpy stop at a certain order:
You have in the numpy exp documentation here that is not very clear about how the Euler number is calculated.
Because of this order that is not equal to infinity, you have this small difference in the two calculations.
Indeed the value np.exp(400) is calculated using this: (1 + 400/n)**n
>>> (1 + 400/n)**n
5.221642085428121e+173
>>> numpy.exp(400)
5.221469689764144e+173
Here you have n = 1000000000000 wich is very small and raise this difference at 10e-5.
Indeed there is no exact value of the Euler number. Like Pi, you can only have an approched value.
It looks like a rounding issue. In the first case it's internally using a very precise value of e, while in the second you get a less precise value, which when multiplied 400 times the precision issues become more apparent.
The actual result when using the Windows calculator is 5.2214696897641439505887630066496e+173, so you can see your first outcome is fine, while the second is not.
5.2214696897641439505887630066496e+173 // calculator
5.221469689764144e+173 // exp(400)
5.221469689764033e+173 // exp(1)**400
Starting from your result, it looks it's using a value with 15 digits of precision.
2.7182818284590452353602874713527 // e
2.7182818284590450909589085441968 // 400th root of the 2nd result
I have to write a code to calculate the number of elements that have the maximum number of divisors between any 2 given numbers (A[0], A[1])(inclusive of both). I have to take input in the form of a line separated with spaces. The first line of the input gives the number of cases present in an example. This code is working perfectly fine but is taking some time to execute. Can anyone please help me write this code more efficiently?
import numpy as np
from sys import stdin
t=input()
for i in range(int(t)):
if int(t)<=100 and int(t)>=1:
divisor=[]
A=list(map(int,stdin.readline().split(' ')))
def divisors(n):
count=0
for k in range(1,int(n/2)+1):
if n%k==0:
count+=1
return count
for j in np.arange(A[0],A[1]+1):
divisor.append(divisors(j))
print(divisor.count(max(divisor)))
Sample input:
2
2 9
1 10
Sample Output:
3
4
There is a way to calculate divisors from the prime factorisation of a number.
Given the prime factorisation, calculating divisors is faster than trial division (which you do here).
But prime factorisation has to be fast. For small numbers having a pre-calculated list of prime numbers (easy to do) can make prime factorisation fast and divisor calculation fast as well. If you konw the upper limit of the numbers you test (let's call it L), then you need the prime numbers up to sqrt(L). Given the prime factorisation of a number n = p_1^e_1 * p_2^e_2 * .. * p_k^e_k the number of divisors is simply (1+e_1) * (1+e_2) * .. * (1+e_k)
Even more, you can pre-calculate and/or memoize the num of divisors of some overused numbers up to some limit. This will save a lot of time but increase memory, else you can calculate it directly (for example using previous method).
Apart from that, you can optimise the code a bit. For example you can avoid doing int(t) casting (and similar) all the time, do it once and store it in a variable.
Numpy may be avoided all together, it is superflous and I doubt adds any speed advantage, depends.
That should make your code faster, but always need to measure performance by real tests.
So I am working on a problem which need me to get factors of a certain number. So as always I am using the module % in order to see if a number is divisible by a certain number and is equal to zero. But when ever I am trying to do this I keep getting an error saying ZeroDivisionError . I tried adding a block of code like this so python does not start counting from zero instead it starts to count from one for potenial in range(number + 1): But this does not seem to work. Below is the rest of my code any help will be appreciated.
def Factors(number):
factors = []
for potenial in range(number + 1):
if number % potenial == 0:
factors.append(potenial)
return factors
In your for loop you are iterating from 0 (range() assumes starting number to be 0 if only 1 argument is given) up to "number". There is a ZeroDivisionError since you are trying to calculate number modulo 0 (number % 0) at the start of the for loop. When calculating the modulo, Python tries to divide number by 0 causing the ZeroDivisionError. Here is the corrected code (fixed the indentation):
def get_factors(number):
factors = []
for potential in range(1, number + 1):
if number % potential == 0:
factors.append(potential)
return factors
However, there are betters ways of calculating factors. For example, you can iterate only up to sqrt(n) where n is the number and then calculate "factor pairs" e.g. if 3 is a factor of 15 then 15/3 which is 5 is also a factor of 15.
I encourage you to try an implement a more efficient algorithm.
Stylistic note: According to PEP 8, function names should be lowercase with words separated by underscores. Uppercase names generally indicate class definitions.
I'm not understanding the reason for subtracting 1 from the the retuen value of randint method to gain a random number, could some one please explain this to me?
import random
def random_item(iterable):
randomIndex= random.randint(0, len(iterable))-1
item=iterable[randomIndex]
return item
randint includes the upper bound:
Return a random integer N such that a <= N <= b.
If you used iterable[len(iterable)] you would get an IndexError so the random.randint(0, len(iterable)) - 1 ensures that if you get a random int equal to the length of the iterable that does not happen.
On a side note, using random.choice(iterable) would be a better idea and remove the need to worry about indexe erros although you have a slightly higher chance of getting the last element with the current implementation which may or may not be intentional.
I am building a python module which will take the two numbers.Then it will find the random numbers between the two numbers for specific number of times and then add all of them a single variable. Then I will use the // operator to find the nearest and least whole number(integer in python). I have just made this much of code:
import random
def randint(minno, maxno, nooftime):
Here the minno is the least no and maxno is the maximum no and nooftime will be the no of times the random numbers will be generated and added to common variable(a , to be specific)
then i will divide a by the nooftime by using this equation (a//nooftime) and then I will print the base quotient
this module will be used for gaming purposes such as generating positions for an enemy to appear and for random map generaion
This basically re-implements random.randint:
from random import random
def randint(minno, maxno, nooftime):
a = sum([random() * (maxno-minno) + minno for _ in range(nooftime)])
return a // nooftime