I'm working on this kata on codewars, which is extended version of famous two egg poblem. Goal is to find the critical floor height with given count of eggs and tries, attempts.
I look it for a little bit an find out that critical floor height is equal to sum of binomial coefficients until the given egg and try count:
Sum of Combination (Tries/i) where i is in range between 1 to Eggs + 1.
I use the comb function from scipy.special with exact=True parameter.
For relatively small numbers, like 477 eggs and 500 tries or 477 eggs and 10000 tires, program calculates very fast, under 0.05 secs. But there are very large checks like 4477 eggs and 10000 tries, which takes about 15 secs.
Is my approach wrong or could i done this calculations without getting execution timeout on codewars with this way?
Here is my script:
import scipy.special
# n: Eggs m: Tries
def height(n,m):
ret = sum(scipy.special.comb(m,i, exact=True) for i in range(1,n+1))
return int(ret)
Since C(N,k) = N! / (k!(N-k)!),
We have C(N,1) = N
And for larger k: C(N,k) = C(N,k-1) * (N-k+1) / k
Calculating C(N,k) is much faster and easier if you start with C(N,k-1).
Let's do it by hand on very small numbers, say 5 and 7. And let me consider also the case of 0 items out of 7, whose value is 1 (you can always subtract 1 from the following if you don't want to include it)
The number of combinations for 0 items out of 7 is 7! / (0! (7-0)!) => 1
The number of combinations for 1 item is 7! / (1! (7-1)!) => 7
2 items => 7! / (2! (7-2)!) => 21. Note we have a new factor 6, since (7-2)! * 6 == (7-1)!, and a new divisor 2 since 1! * 2 == 2!. At the previous iteration we had a factor 7 and a divisor 1 of course
3 items => 35. New factor 5, new divisor 3
4 items => 35 again. New factor and new divisor are both 4
5 items => 21 again. Now new factor and new divisor are inverted (3 and 5)
Should we continue we would get
6 items => 7
7 items => 1.
So the total number of combinations up to 5 items out of 7 is 1 + 7 + 21 + 35 + 35 + 21 => 120. Note that if we count up to 7 we get 128, i.e. 2**7 - 1
So the simplest code can be:
def sum_of_combos(n,k):
mult = k
total = 1
currval = 1
for i in range(1,n+1):
currval = (currval * mult) // i
total += currval
mult -= 1
return total
>>> timeit("sum_of_combos(4477,10000)", globals=globals(), number=1000)
17.124391099999997
>>> timeit("sum_of_combos(8477,10000)", globals=globals(), number=1000)
36.44438840000001
Is there room for optimizations? Yes, there is. For instance in the second example above instead of iterating 8477 times we could calculate 2**10000 and then subtract from that the first (10000 - 8477) values.
I found out instead of using scipy, using loop is more efficient. And implemented this:
# n: Eggs m: Tries
def height(n,m):
if n == 0 or m == 0:
return 0
if n > m:
n = m
counter,current,total = 1,1,0
while counter <= n:
d = (m+1-counter) * current // counter
total,current,counter = total+d,d,counter+1
return total
It is very fast compared to my previous attempt. On 4477,10000 this takes 0.02 secs to complete, previous one toke 16.97 secs to complete.
I need to find the sum of all even numbers below the inserted number. For example if I insert 8 then the sum would be 2+4+6+8=20. If I insert 9 then it also needs to be 20. And it needs to be based on recursion.
This is what I have so far:
def even(a):
if a == 0:
else:
even(a - 1)
even(8)
I cannot figure out what to change under the "if" part for it to give the right outcome
If the function is called with an odd number, n, then you can immediately call again with the number below (an even).
Then if the function is called with an even number return that even number plus the result of summing all the even numbers below this number by calling again with n - 2.
Finally, your base case occurs when n = 0. In this case just return 0.
So we have
def even_sum(n):
if n % 2 == 1: # n is odd
return even_sum(n - 1)
if n == 0:
return 0
return n + even_sum(n - 2)
which works as expected
>>> even_sum(8)
20
>>> even_sum(9)
20
>>> even_sum(0)
0
To design a recursive algorithm, the first thing to wonder is "In what cases can my algorithm return an answer trivially?". In your case, the answer is "If it is called with 0, the algorithm answers 0". Hence, you can write:
def even(n):
if n == 0:
return 0
Now the next question is "Given a particular input, how can I reduce the size of this input, so that it will eventually reach the trivial condition?"
If you have an even number, you want to have this even number + the sum of even numbers below it, which is the result of even(n-2). If you have an odd number, you want to return the sum of even numbers below it. Hence the final version of your function is:
def even(n):
if n == 0 or n == 1:
return 0
if n % 2 == 0:
return n + even(n - 2)
return even(n - 1)
Both with o(n) time complexity
With For loop
num = int(input("Enter a number: ")) # given number to find sum
my_sum = 0
for n in range(num + 1):
if n % 2 == 0:
my_sum += n
print(my_sum)
With recursion
def my_sum(num):
if num == 0:
return 0
if num % 2 == 1:
return my_sum(num - 1)
return num + my_sum(num - 2)
always avoid O(n^2) and greater time complexity
For a recursive solution:
def evenSum(N): return 0 if N < 2 else N - N%2 + evenSum(N-2)
If you were always given an even number as input, you could simply recurse using N + f(N-2).
For example: 8 + ( 6 + (4 + ( 2 + 0 ) ) )
But the odd numbers will require that you strip the odd bit in the calculation (e.g. subtracting 1 at each recursion)
For example: 9-1 + ( 7-1 + ( 5-1 + ( 3-1 + 0 ) ) )
You can achieve this stripping of odd bits by subtracting the modulo 2 of the input value. This subtracts zero for even numbers and one for odd numbers.
adjusting your code
Your approach is recursing by 1, so it will go through both the even and odd numbers down to zero (at which point it must stop recursing and simply return zero).
Here's how you can adjust it:
Return a value of zero when you are given zero as input
Make sure to return the computed value that comes from the next level of recursion (you are missing return in front of your call to even(a-1)
Add the parameter value when it is even but don't add it when it is odd
...
def even(a):
if a == 0 : return 0 # base case, no further recusion
if a%2 == 1 : return even(a-1) # odd number: skip to even number
return a + even(a-1) # even number: add with recursion
# a+even(a-2) would be better
A trick to create a recursive function
An easy way to come up with the structure of a recursive function is to be very optimistic and imagine that you already have one that works. Then determine how you would use the result of that imaginary function to produce the next result. That will be the recursive part of the function.
Finally, find a case where you would know the answer without using the function. That will be your exit condition.
In this case (sum of even numbers), imagine you already have a function magic(x) that gives you the answer for x. How would you use it to find a solution for n given the result of magic(n-1) ?
If n is even, add it to magic(n-1). If n is odd, use magic(n-1) directly.
Now, to find a smaller n where we know the answer without using magic(). Well if n is less than 2 (or zero) we know that magic(n) will return zero so we can give that result without calling it.
So our recursion is "n+magic(n-1) if n is even, else magic(n-1)"
and our stop condition is "zero if n < 2"
Now substitute magic with the name of your function and the magic is done.
For an O(1) solution:
Given that the sum of numbers from 1 to N can be calculated with N*(N+1)//2, you can get half of the sum of even numbers if you use N//2 in the formula. Then multiply the result by 2 to obtain the sum of even numbers.
so (N//2)*(N//2+1) will give the answer directly in O(1) time:
N = 8
print((N//2)*(N//2+1))
# 20
# other examples:
for N in range(10):
print(N,N//2*(N//2+1))
# 0 0
# 1 0
# 2 2
# 3 2
# 4 6
# 5 6
# 6 12
# 7 12
# 8 20
# 9 20
Visually, you can see the progression like this:
1..n : 1 2 3 4 5 6 7 8
∑n : 1 3 6 10 15 21 28 36 n(n+1)/2
n/2 : 0 1 1 2 2 3 3 4
1..n/2 : 1 2 3 4
∑n/2 : 1 3 5 10 half of the result
2∑n/2 : 2 6 10 20 sum of even numbers
So we simply replace N with N//2 in the formula and multiply the result by 2:
N*(N+1)//2 --> replace N with N//2 --> N//2*(N//2+1)//2
N//2*(N//2+1)//2 --> multiply by 2 --> N//2*(N//2+1)
Another way to see it is using Gauss's visualisation of the sum of numbers but using even numbers:
ascending 2 4 6 8 ... N-6 N-4 N-2 N (where N is even)
descending N N-2 N-4 N-6 ... 8 6 4 2
--- --- --- --- --- --- --- ---
totals N+2 N+2 N+2 N+2 ... N+2 N+2 N+2 N+2 (N/2 times N+2)
Because we added the even numbers twice, once in ascending order and once in descending order, the sum of all the totals will be twice the sum of even numbers (we need to divide that sum by 2 to get what we are looking for).
sum of evens: N/2*(N+2)/2 --> N/2*(N/2+1)
The N/2(N/2+1) formulation allows us to supply the formula with an odd number and get the right result by using integer division which absorbs the 'odd bit': N//2(N//2+1)
Recursive O(1) solution
Instead of using the integer division to absorb the odd bit, you could use recursion with the polynomial form of N/2*(N+2)/2: N^2/4 + N/2
def sumEven(n):
if n%2 == 0 : return n**2/4 + n/2 # exit condition
return sumEven(n-1) # recursion
Technically this is recursive although in practice it will never go deeper than 1 level
Try out this.
>>> n = 5
>>> sum(range(0, n+1, 2))
with minimum complexity
# include <stdio.h>
void main()
{
int num, sum, i;
printf("Number: ");
scanf("%d", &num);
i = num;
if (num % 2 != 0)
num = num -1;
sum = (num * (num + 2)) / 4;
printf("The sum of even numbers upto %d is %d\n\n", i, sum);
}
It is a C program and could be used in any language with respective syntax.
And it needs to be based on recursion.
Though you want a recursion one, I still want to share this dp solution with detailed steps to solve this problem.
Dynamic Programming
dp[i] represents the even sum among [0, i] which I denote as nums.
Case1: When i is 0, there is one number 0 in nums. dp[0] is 0.
Case2: When i is 1, there are two numbers 0 and 1 in nums. dp[1] is still 0.
Case3: When i is 2, there are three numbers 0, 1 and 2 in nums. dp[2] is 2.
Case4: When i is greater than 2, there are two more cases
If i is odd, dp[i] = dp[i-1]. Since i is odd, it is the same with [0, i-1].
If i is even, dp[i] = dp[i-2] + i by adding the current even number to the even sum among [0, i-2] (i-1 is odd, so won't be added).
PS. dp[i] = dp[i-1] + i is also ok. The difference is how you initialize dp.
Since we want the even sum among [0, n], we return dp[n]. You can conclude this from the first three cases.
def even_sum(n):
dp = []
# Init
dp.append(0) # dp[0] = 0
dp.append(0) # dp[1] = 0
# DP
for i in range(2, n+1): # n+1 because range(i, j) results [i, j) and you take n into account
if i % 2 == 1: # n is odd
dp.append(dp[i-1]) # dp[i] = dp[i-1]
else: # n is even
dp.append(dp[i-2] + i) # dp[i] = dp[i-2] + i
return dp[-1]
I am trying to make a decimal to binary converter, however, I noticed that some values are being ignored and the last value is not being entered into the list I created.
#Here we create a new empty list.
binary = []
n = int(input("Enter number: "))
while n > 1:
n = n//2
m = n%2
binary.append(m)
binary.reverse()
print( " ".join( repr(e) for e in binary ))
This is your code after correction :
binary = []
n = int(input("Enter number: "))
while n > 0:
m = n%2
n = n//2
binary.append(m)
if len(binary)==0:
binary.append(0)
binary.reverse()
print( " ".join( repr(e) for e in binary ))
Your question is duplicate to this stackoverflow question check the link too.
good luck :)
As PM 2Ring suggested a tuple assignment may be the way to go. Makes your code shorter too :-) ... also changed n > 1 to n >= 1
binary = []
n = int(input("Enter number: "))
while n >= 1:
n, m = n // 2, n % 2
binary.append(m)
binary.reverse()
print( " ".join( repr(e) for e in binary ))
n = int(input("Enter number: "))
print("{0:0b}".format(n)) # one-line alternate solution
if n == 0: # original code with bugs fixed
binary = [0]
else:
binary = []
while n > 0:
m = n%2
n = n//2
binary.append(m)
binary.reverse()
print("".join( repr(e) for e in binary ))
Your algorithm is close, but you need to save the remainder before you perform the division. And you also need to change the while condition, and to do special handling if the input value of n is zero.
I've fixed your code & put it in a loop to make it easier to test.
for n in range(16):
old_n = n
#Here we create a new empty list.
binary = []
while n:
m = n % 2
n = n // 2
binary.append(m)
# If the binary list is empty, the original n must have been zero
if not binary:
binary.append(0)
binary.reverse()
print(old_n, " ".join(repr(e) for e in binary))
output
0 0
1 1
2 1 0
3 1 1
4 1 0 0
5 1 0 1
6 1 1 0
7 1 1 1
8 1 0 0 0
9 1 0 0 1
10 1 0 1 0
11 1 0 1 1
12 1 1 0 0
13 1 1 0 1
14 1 1 1 0
15 1 1 1 1
As Blckknght mentions in the comments, there's a standard function that can give you the quotient and remainder in one step
n, m = divmod(n, 2)
It can be convenient, but it doesn't really provide much benefit apart from making the code a little more readable. Another option is to use a tuple assignment to perform the operations in parallel:
n, m = n // 2, n % 2
It's a good practice, especially when you're new to coding, to work through your algorithm on paper to get a feel for what it's doing. And if your code doesn't give the expected output it's a good idea to add a few print calls in strategic places to check that the values of your variables are what you expect them to be. Or learn to use a proper debugger. ;)
So I came across this question:
How many numbers are there from 1 to 1000 which are not divisible by the digits 2, 3 and 5?
It seems pretty easy at first, so I wrote a quick python program to solve it:
count = 0
for number in range(1,1000):
if number % 2 != 0 and number % 3 != 0 and number % 5 != 0:
count += 1
print(count)
I got the correct answer (266), but I thought that doing it that way was a lot of typing if I ever wanted to check more than just 3 values. I also wanted to do a mathematical solution so I came across this:
1000 - ((1000/2 +1000/3 +1000/5) -(1000/2x3 +1000/2x5 + 1000/3x5)+ (1000/2x3x5)) = 1000-((500+333+200) - (166 +100 + 66) + 33) = 1000- 734 = 266
I thought it was a good approach so I implemented it in code:
def foo(ln = 1000), numbers = [2,3,5]:
div = 0
muldiv = 0
totdiv = 1
for n in numbers:
div += ln/n
for i in numbers:
for n in range(numbers.index(i)+1, len(numbers)):
muldiv += ln/(i * numbers[n])
for n in numbers:
totdiv *= n
answer = ln - (div - muldiv + ln/totdiv)
print("answer is ", math.floor(answer))
Now I am pretty sure I messed up somewhere in my second function because it doesn't seem to work for more numbers. For example, if I were to try to find
How many numbers are there from 1 to 1000 which are not divisible by the digits 2, 3, 5 and 7?
the first method returns 228 and foo(numbers = [2,3,5,7]) returns 300... I'm pretty sure 228 is the correct answer since one more number would mean that there are LESS factors instead of more, but where did I go wrong? and is there a better way to solve this problem?
You do not need an algorithm for that, simple mathematics is enough:
Say you want to count the amount of numbers from 1 to N (inclusive) dividable by k, that is simply equivalent to:
floor(N/k).
So the amount of numbers dividable by 3 in this case is 333.
Now you can't however simply use calculate the amount of numbers dividable by 2, 3 and 5; and sum them up, because there are common ones. Indeed: for instance 15 is dividable by both 3 and 5.
You can solve this however using the inclusion-exclusion principle:
the amount of numbers dividable by 2, 3 and 5 is the same as
the amount numbers dividable by 2
plus the amount of numbers dividable by 3
plus the amount of numbers dividable by 5
minus the amount of numbers dividable by 2 and 3
minus the amount of numbers dividable by 2 and 5
minus the amount of numbers dividable by 3 and 5
plus the amount of numbers dividable by 2, 3 and 5.
So in order to solve your first problem, you can simply state:
def n_div(N,k):
return N//k
def n_div235(N):
return n_div(N,2)+n_div(N,3)+n_div(N,5)-n_div(N,2*3)-n_div(N,2*5)-n_div(N,3*5)+n_div(N,2*3*5)
def not_div235(N):
return N-n_div235(N)
As you can see it generates the correct result:
>>> not_div235(1000)
266
As long as N is very large compared to the number of divisors, you better use the inclusion-exclusion approach:
you can do this like:
import itertools
from functools import reduce
import operator
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
def lcm_list(ks):
res = 1
for k in ks:
res = lcm(res,k)
return res
def n_div_gen(N,ks):
nk = len(ks)
sum = 0
factor = 1
for i in range(1,nk+1):
subsum = 0
for comb in itertools.combinations(ks,i):
subsum += n_div(N,lcm_list(comb))
sum += factor * subsum
factor = -factor
return sum
def not_div_gen(N,ks):
return N-n_div_gen(N,ks)
For small N, this will not pay off, but say to want to calculate the amount of numbers dividable by 3, 5 and 7 from 1 to 1 000 000 000 is:
>>> not_div_gen(1000000000,[3,5,7])
457142857
You can do this with:
>>> sum(i%3!=0 and i%5!=0 and i%7!=0 for i in range(1,1000000001))
457142857
But it takes minutes to calculate that whereas our own approach uses milliseconds. Note that this only works for a huge N.
Use the built-in functions sum and all with a nested generator:
def f(r=1000, nums=(2,3,5)):
return sum(all(x%n for n in nums) for x in range(1, r+1))
This goes through the range of numbers, check whether each of those numbers has a nonzero modulus with each of the specified numbers, and sums those boolean values (False is 0 and True is 1). A nums of (2,3,5,7) produces a result of 228, which is in agreement with your shorter, simpler code (which, reassuringly, doesn't use any floating-point arithmetic, as your second code block does).
The number of integers up to N not divisible by n1,n2,...,nt (assumed to be pairwise-coprime) is
the number of integers up to N minus
( SUMi in 1..t ( the number of integers up to N divisible by ni)) plus
( SUMi,j in 1..t, i<j ( the number of integers up to N divisible by ninj)) minus
( SUMi,j,k in 1..t, i<j<k ( the number of integers up to N divisible by ninjnk)) plus
( SUMi,j,k,l in 1..t, i<j<k<l ( the number of integers up to N divisible by ninjnknl)) minus
... ... ... ...
( SUMi,j,k,l,...q in 1..t, i<j<k<l<...<q ( the number of integers up to N divisible by ninjnknl...nq))
The series continues until the subscript contains all t integers from the original list.
For numbers that are not known to be pairwise-coprime, replace their product by the least common multiple.
This is why your method works only for 3 numbers. You only compute the first four members of the series.
Here's another implementation that uses inclusion-exclusion. It's simpler than the code in Willem Van Onsem's excellent answer (which I didn't see before I wrote this code), but this one only works if the numbers in the list of divisors are all coprime to each other. For the the more general case, you need to use Willem's approach.
from itertools import combinations
from functools import reduce
def prod(seq, mul=int.__mul__):
return reduce(mul, seq, 1)
def count_coprimes(n, divisors):
total = n
sign = -1
for i in range(1, len(divisors) + 1):
for k in combinations(divisors, i):
total += n // prod(k) * sign
sign = -sign
return total
print(count_coprimes(1000, [2, 3, 5]))
output
266
FWIW, here's the same algorithm as a "one-liner" (split over several lines to improve readability). It's a little less efficient due to the (-1)**i in the inner loop.
def count_coprimes(n, divisors):
return n + sum(n // prod(k) * (-1)**i
for i in range(1, len(divisors) + 1)
for k in combinations(divisors, i))
print(count_coprimes(1000000000, [3, 5, 7]))
output
457142857
We can get rid of that (-1)**i by negating the divisors and using a modified integer division function:
def div(p, q):
return p // q if q > 0 else -(p // -q)
def count_coprimes(n, divisors):
return sum(div(n, prod(k))
for i in range(len(divisors) + 1)
for k in combinations([-u for u in divisors], i))
A very small change you can make to roughly halve the amount of time that it takes is rather than generating all numbers from 1 to 1000, generate all odd numbers from 1 to 1000:
count = 0
for number in range(1,1001,2):
if number % 3 != 0 and number % 5 != 0:
count += 1
print(count)
While this is not a huge change and is not a mathematical solution it makes the code no less readable and slightly more efficient.
And to take into account your other code, you can use a list comprehension in the if statement to check other numbers(note that I also use the first number to generate the initial list of numbers, rather than performing the modulo operation for all 1000):
def foo(lst):
count = 0
for number in range(1,1001,lst[0]):
if not any([number % i == 0 for i in lst[1:]]):
count += 1
return count
>>> foo([2,3,5])
266
>>> foo([2,3,5,7])
228
There are many ways to solve this little problem iteratively, all of them with pretty much similar performance, here's few examples:
import timeit
def f1(l, h):
count = 0
for number in range(l, h):
if number % 2 != 0 and number % 3 != 0 and number % 5 != 0:
count += 1
return count
def f2(l, h):
return len(filter(lambda x: x % 2 != 0 and x % 3 != 0 and x % 5 != 0, range(l, h)))
def f3(l, h):
count = 0
for number in range(l, h):
if number % 2 == 0:
continue
if number % 3 == 0:
continue
if number % 5 == 0:
continue
count += 1
return count
def f4(l, h):
return len([x for x in range(l, h) if x % 2 != 0 and x % 3 != 0 and x % 5 != 0])
a, b, N = 1, 1000, 10000
print timeit.timeit('f1(a,b)', setup='from __main__ import f1, a, b', number=N)
print timeit.timeit('f2(a,b)', setup='from __main__ import f2, a, b', number=N)
print timeit.timeit('f3(a,b)', setup='from __main__ import f3, a, b', number=N)
print timeit.timeit('f4(a,b)', setup='from __main__ import f4, a, b', number=N)
Times on a i7-2.6ghz would be:
0.802361558825
1.46568073638
0.91737188946
0.846404330893
Usually these times are good enough to be considered when the lower/upper bounds (1,1000) are relatively small. Now, if we're talking about really high bounds (trillions) where the computation is not feasible you could consider apply the much smarter inclusion-exclusion principle, that way you'd solve the problem analitically and you'd be granted to get constant time with your solution.
input
let n be the interval <0,n> to test and d[]={2,3,5,0}; be null terminated array of divisors
compute LCM of d[]
least common multiple is the period with which the SoE will repeat itself. for 2,3,5 is lcm=30. Use the max(d[]) as increment while computing it to boost speed... If LCM is too big (LCM>=n) then use n instead for speed.
compute SoE for <0,LCM)
simply create array of LCM numbers and set a[i]=1 for non divisible i and a[i]=0 for divisible i.
convert SoE to non divisible number count
simply compute a'[i]=a[0]+a[1]+..a[i]
compute the count
count is simply:
int(n/LCM)*a'[LCM-1] + a'[n%LCM];
Here simple C++ example:
int non_divisibles(int n,const int *d) // SoE
{
int i,j,cnt,lcm,m,*a;
for (m=0;d[m];m++); // compute m
for (cnt=0,i=0;i<m;i++) if (cnt<d[i]) cnt=d[i]; // cnt = max d[] (to speed up LCM)
// LCM d[]
a=new int[m]; if (a==NULL) return -1;
for (i=0;i<m;i++) a[i]=d[i];
for (lcm=cnt;lcm<=n;lcm+=cnt) // no need to test above `n` even if lcm is bigger
{
for (i=0;i<m;i++) for (;a[i]<lcm;) a[i]+=d[i];
for (i=0;i<m;i++) if (a[i]!=lcm) { i=-1; break; }
if (i>=0) break;
}
delete[] a;
// SoE <0,LCM)
a=new int[lcm]; if (a==NULL) return -1;
for (i=0;i<lcm;i++) a[i]=1;
for (j=0;d[j];j++)
for (i=0;i<lcm;i+=d[j])
a[i]=0;
// convert to cnt
for (i=1;i<lcm;i++) a[i]+=a[i-1];
// compute whole count
cnt =(n/lcm)*a[lcm-1];
cnt+=a[n%lcm];
delete[] a;
return cnt;
}
Here some measurements to compare naive,SoE and this SoE(max(n,LCM(d[]))) approaches:
n=1000000 d[]={ 2 3 5 7 11 13 17 19 }
171021 [ 27.514 ms] naive
171021 [ 12.642 ms] SoE
171021 [ 25.438 ms] LCM+Soe
n=1000000 d[]={ 2 3 5 7 11 13 17 }
180524 [ 26.212 ms] naive
180524 [ 11.781 ms] SoE
180524 [ 9.807 ms] LCM+Soe
n=1000000 d[]={ 2 3 5 7 11 13 }
191808 [ 24.690 ms] naive
191808 [ 11.512 ms] SoE
191808 [ 0.702 ms] LCM+Soe
n=1000000 d[]={ 2 3 5 }
266666 [ 16.468 ms] naive
266666 [ 9.744 ms] SoE
266666 [ 0.006 ms] LCM+Soe
n= 1000 d[]={ 2 3 5 }
266 [ 0.012 ms] naive
266 [ 0.012 ms] SoE
266 [ 0.001 ms] LCM+Soe
n=1000000 d[]={ 2 3 5 19 23 61 87 10001 }
237662 [ 26.493 ms] naive
237662 [ 10.180 ms] SoE
237662 [ 19.429 ms] LCM+Soe
As you can see SoE(n) is better if LCM is too big in comparison to n(d[] contains many primes or big numbers) but need O(n) space.
You can bail out as soon as you hit a divisible.
def test(tocheck):
count = 0
for number in range(1, 1000):
for div in tocheck:
if not number % div:
break
else:
#else on a loop means it ran to completion w.o. break
count += 1
print("%d not divisible by %s" % (count, tocheck))
test([2,3,5])
test([2,3,5,7])
output:
266 not divisible by [2, 3, 5]
228 not divisible by [2, 3, 5, 7]
def not_divisible(n = 1000, divisors = [2, 3, 5]):
count = 0
for i in range(1, n + 1):
if all(1 if i % d else 0 for d in divisors):
count += 1
return count
Explanation for 4th line:
If number i is not divisible by divisor d, i % d returns a non-zero
integer. Python considers any non-zero number as True.
List comprehension [1 if i % d else 0 for d in divisors] returns a
list of 1s and 0s such that, if number is not divisible, the value is
1 , else 0.
The all() function checks if all values in a collection are True. If
all values in the list are 1 (True), meaning the number is 'not
divisible' by all divisors.
If number is not divisible by both 2 and 3, it cannot be divisible by
6. So no need to check for that here.
Here's a smaller code:
def not_divisible(n = 1000, divisors = [2, 3, 5]):
return sum(1 for i in range(1, n + 1) if all(1 if i % d else 0 for d in divisors))
Here we're generating a list of 1s for every number not divisible by all divisors and sum of that list is the answer.
I was working on a problem in Project Euler; and I found a question in SO. The question and accepted answer says;
n = 600851475143
i = 2
while i * i < n:
while n%i == 0:
n = n / i
i = i + 1
print (n)
It's just awesome. I still can't understand how this process is so fast and can find the largest prime factor of 600billion in 0.00001 seconds. I tried tons of methods and codes for that, processes took over than 1 hour..
Could anyone explain me the logic of this codes and why it's super-fast? is while loop have a special place in Python?
The fundamental theorem of arithmetic states that every integer greater than 1 can be represented as the product of prime numbers. E.g., the number 2100 can be represented like so:
2 x 2 x 3 x 5 x 5 x 7
I've arranged it so the largest prime factor is on the right, in this case 7. What this algorithm is doing is starting from 2 and dividing n (i.e. "removing" that factor) until there are no more to remove (the modulo 0 step checks that it is divisible cleanly before dividing.)
So, following the code, we would have i = 2 and n = 2100, or
2 x 2 x 3 x 5 x 5 x 7
2100 is divisible by 2 (2100 % 2 == 0) and also because we see a 2 in the factorization above. So divide it by 2 and we get 1050, or
2 x 3 x 5 x 5 x 7
Continue dividing by 2, once more, and you get a number that is no longer divisible by 2, that is 525, or
3 x 5 x 5 x 7
Then we increase i to 3 and continue. See how by the end we will be left with the highest prime factor?
The reason for the first while loop's i * i < n (which really should be i * i <= n) is because
if one divisor or factor of a number (other than a perfect square) is greater than its square root, then the other factor will be less than its square root. Hence all multiples of primes greater than the square root of n need not be considered.
from: http://britton.disted.camosun.bc.ca/jberatosthenes.htm
So if i is greater than the square root of n, that means all remaining factors would have had a "pair" that we already found, below the square root of n. The check used, i * i <= n is equivalent but faster than doing a square root calculation.
The reason this is so quick and other brute force methods are so slow is because this is dividing the number down in each step, which exponentially cuts the number of steps that need to be done.
To see this, the prime factorization of 600851475143 is
71 x 839 x 1471 x 6857
and if you modify the code to read:
n = 600851475143
i = 2
while i * i <= n:
while n%i == 0:
print "Dividing by %d" % i
n = n / i
i = i + 1
if n > 1:
print n
You'll see:
>>>
Dividing by 71
Dividing by 839
Dividing by 1471
6857
which shows you that this is exactly how it's working.