Given any random integer, create a function to find the next number that is a prime number and also a palindrome.
My attempt
def golf(number):
x = number + 1
for i in range(2, x):
if x % i == 0 or str(x) != str(x)[::-1]:
golf(number + 1)
return x
E.g golf(13) = 101
I'm actually looking for an alternative option than the recursion method i used. How can this best be accomplished without using recursion?
Thanks
Here's a variation on byron he's answer which adds several optimizations:
We can eliminate all even x values (other than 2) before doing any elaborate tests, since we can trivially tell they are not prime.
A small improvement is to only call str(x) once, and reuse the value later.
We can take advantage of the fact that all even-length palindromes are multiples of 11, which means that (except for 11 itself) they're not prime. We can jump ahead to the next odd-length x value.
Since we've already eliminated even numbers, our prime test only needs to test odd divisors. Further we can stop our loop when we reach sqrt(x), rather than going all the way to x itself.
Finally, there's no need to use a Boolean flag variable to carry the primeness out of the loop. If we don't break, the else block attached to the loop will be run.
The code:
import math
def next_prime_palindrome(x):
while True:
x += 1
if x > 2 and x % 2 == 0: # even numbers greater than 2 are non-prime
continue
s = str(x) # compute str(x) just once
if x > 11 and len(s) % 2 == 0: # all even-length palindromes are multiples of 11
x = 10 ** len(s) # so jump to the next odd-length integer
continue
if s != s[::-1]: # palindrome test
continue
for i in xrange(3, round(math.sqrt(x))+1, 2): # loop over odd potential divisors
if x % i == 0: # prime test
break
else: # this else block runs only if no break happened in the loop, so x is prime
return x
Here are some tests runs, showing a few cases where the optimizations save significant time:
>>> next_prime_palindrome(1)
2
>>> next_prime_palindrome(3)
5
>>> next_prime_palindrome(9)
11
>>> next_prime_palindrome(11)
101
>>> next_prime_palindrome(99999)
1003001
>>> next_prime_palindrome(999999999)
10000500001
A further improvement might be to directly generate palindromes, rather than working with integers to start with, and doing a palindrome test to filter them. That would get quite a bit further from your original design, so I'll leave that for someone else.
Palindrome are a sparser set of numbers than primes, and you can generate palindromes directly.
Consider the sequence 98.102
These are palidrome numbers you can base on these
989, 9889, 999, 9999, 10001, 100001, 10101, 101101, 10201, 102201
ADDED
Not also that all of the palidromes with an odd number of digits will come before the palidromes with an even number of digits.
If you write this as a generator (ie using yield) get get a straightforward algorithm for generating palindromic numbers in order.
For 1..9 you generate either 9 or 18 palindromes depending upon whether you consider 1 digit numbers palindromic.
For 10..99 you generate 90 even digit and 90 odd digit palindromes.
For 100..999 you generate 900 even digit and 900 odd digit palindromes.
You have just generated all 1989 (or 1997 if including single digit numbers) of the palindromic numbers less than 1 million. There are 78,498 primes less than 1 million
Any algorithm that is based on generating primes then testing for a palindrome will be much slower that generating palindromes and then testing for primes
def golf(number):
primes = []
i = 2
while i <= number:
if isPrime(i, primes):
primes.append(i)
i += 1
answer = primes[-1] + 1
while True:
if isPrime(answer, primes):
primes.append(answer)
if str(answer) == str(answer)[::-1]:
return answer
answer += 1
def isPrime(n, primes):
for (p for p in primes if p<=n**0.5):
if n%p == 0:
return False
return True
Your solution can be slightly modified in order to create an iterative solution:
def golf(number):
x = number + 1
while True:
is_golf = True
for i in range(2, x):
if x % i == 0 or str(x) != str(x)[::-1]:
is_golf = False
break
if is_golf:
return x
x += 1
improved according to Blckknght's advice, thanks.
def golf(number):
x = number
while True:
x += 1
if str(x) != str(x)[::-1]:
continue
for i in xrange(2, x):
if x % i == 0 :
break
else:
return x
This question already has answers here:
`xrange(2**100)` -> OverflowError: long int too large to convert to int
(5 answers)
Closed 13 days ago.
I am just starting to learn to code in Python. I am trying to write some code to answer this Project Euler Question:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
My program works with the test case of 13195, but when I try to enter 600851475143, I get the error: "OverflowError: range() results has too many items"
Does anyone know how I can fix this?
Here is my code:
class Euler3:
"A class to find the largest prime factor of a given number"
n = 600851475143
primeFactors = []
for i in range(2,n):
if (n%i ==0):
primeFactors.append(i)
n = n/i
i = i -1 #reset i
print primeFactors
Any help or suggestions would be much appreciated!
The range function creates a list and tries to store it in memory. Creating a list many numbers long is what's causing the OverflowError. You can use xrange instead to get a generator which produces the numbers on demand.
That said, I think you'll find that your algorithm is way too slow for calculating large primes. There are a lot of prime number algorithms, but I might suggest checking out the Sieve of Eratosthenes as a starting point.
EDIT: Properly xrange actually doesn't return a generator, but an xrange object which behaves a lot like a generator. I'm not sure if you care, but it was bugging me that i wasn't precise!
I'm guessing you're using python 2 and not python 3 -- range(2,n) actually constructs a list! You don't have enough memory to store 600 billion numbers! xrange should be fine, though.
Also, your idea of i=i-1 doesn't work. For loops don't work like C, and that hack only works in C-style loops. The for loop iterates over range(2,n). If i gets the value 5 at once iteration, then no matter what you do to i, it still gets 6 the next time through.
Also, the list range(2,n) is constructed when you enter the loop. So when you modify n, that doesn't change anything.
You're going to have to rethink your logic a bit.
(if you don't believe me, try using 175 as a test case)
As a last comment, you should probably get in the habit of using the special integer division: n = n // i. Although / and // work the same in python 2, that is really deprecated behavior, and they don't work the same in python 3, where / will give you a floating point number.
You can fix the problem by using xrange instead of range
Your next problem will be that the program takes way too long to run because you need to break out of the loop under some condition
A better way to deal with repeat factors is to replace the if with a while
while (n%i ==0):
primeFactors.append(i)
n = n/i
n = 600851475143
primeFactors = []
for i in range(2,n):
i think you can optimize the function by noticing that
for i in range(2,n):
you can replace
range(2,n)
by
range(2,int(sqrt(n))+2)
because, you can see wiki...
This is the best way to find primes that I've found so far:
def isprime(n):
#make sure n is a positive integer
n = abs(int(n))
#0 and 1 are not primes
if n < 2:
return False
#2 is the only even prime number
if n == 2:
return True
#all other even numbers are not primes
if not n & 1:
return False
#range starts with 3 and only needs to go up to the square root of n
#for all odd numbers
for x in range (3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True #if it makes it through all the catches, it is a prime
This took me about 5 secs to get the answer.
import math
def is_prime_number(x):
for i in range(int(math.sqrt(x)), 2, -1):
if x % i == 0:
return False
return True
def next_prime_number(number):
#Returns the next prime number.
if number % 2 == 0 and number != 2:
number += 1
while not is_prime_number(number):
number += 2
return number
num = 600851475143
i = 2
while (i < long(math.sqrt(num) / 2)):
if num % i == 0:
largest = i
print largest
i = next_prime_number(i + 1)
xrange won't probably help you(or it may!),but the key thing here is to reliaze that you don't need to find the prime numbers up till 600851475143 or the factors of 600851475143,but it's prime factors,so...
Use the good old prime factorization method ,like so:
A=600851475143
n=2
fac=[]
while(n<=int(A)):
B=1
while(A%n==0):
B=0
A=A/n
if(B==0):
fac.append(n)
n=n+1
print max(fac)
This will return the argest prime factor almost instantly
I was battling with this Python "OverflowError", as well, working on this project. It was driving me nuts trying to come up with a combination that worked.
However, I did discover a clever, at least I think so :), way to do it.
Here is my code for this problem.
def IsNumberPrime(n):
bounds = int(math.sqrt(n))
for number in xrange(2,bounds+2):
if n % number == 0:
return False
return True
def GetListOfPrimeFactors(n):
primes = []
factors = GetListOfFactors(n)
if n % 2 == 0:
primes.append(2)
for entries in factors:
if IsNumberPrime(entries):
primes.append(entries)
return primes
GetListOfPrimeFactors(600851475143)
def GetListOfFactors(n):
factors = []
bounds = int(math.sqrt(n))
startNo = 2
while startNo <= bounds:
if n % startNo == 0:
factors.append(startNo)
startNo += 1
return factors
What I did was take the factors of the number entered and enter them into a list "factors". After that, I take the list of factors and determine which ones are primes and store them into a list, which is printed.
I hope this helps
-- Mike
I've recently been working on Project Euler problems in Python. I am fairly new to Python, and still somewhat new as a programmer.
In any case, I've ran into a speed-related issue coding a solution for problem #5. The problem is,
"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?"
I've checked around some, and I haven't been able to find anything on this problem pertaining to Python specifically. There were some completed scripts, but I want to avoid looking at other's code in full, if possible, instead wanting to improve my own.
The code I have written runs successfully for the example of 2520 and the range 1 to 10, and should be directly modifiable to work with the question. However, upon running it, I do not get an answer. Presumably, it is a very high number, and the code is not fast enough. Printing the current number being checked seems to support this, reaching several million without getting an answer.
The code, in it's current implementation is as follows:
rangemax = 20
def div_check(n):
for i in xrange(11,rangemax+1):
if n % i == 0:
continue
else:
return False
return True
if __name__ == '__main__':
num = 2
while not div_check(num):
print num
num += 2
print num
I have already made a couple changes which I think should help the speed. For one, for a number to be divisible by all numbers 1 to 20, it must be even, as only even numbers are divisible by 2. Hence, I can increment by 2 instead of 1. Also, although I didn't think of it myself, I found someone point out that a number divisible by 11 to 20 is divisible by 1 to 10. (Haven't checked that one, but it seems reasonable)
The code still, however is not fast enough. What optimisations, either programmatic, or mathematics, can I make to make this code run faster?
Thanks in advance to any who can help.
Taking the advice of Michael Mior and poke, I wrote a solution. I tried to use a few tricks to make it fast.
Since we need a relatively short list of numbers tested, then we can pre-build the list of numbers rather than repeatedly calling xrange() or range().
Also, while it would work to just put the numbers [1, 2, 3, ..., 20] in the list, we can think a little bit, and pull numbers out:
Just take the 1 out. Every integer is evenly divisible by 1.
If we leave the 20 in, there is no need to leave the 2 in. Any integer evenly divisible by 20 is evenly divisible by 2 (but the reverse might not be true). So we leave the 20 and take out the 2, the 4, and the 5. Leave the 19, as it's prime. Leave the 18, but now we can take out the 3 and the 6. If you repeat this process, you wind up with a much shorter list of numbers to try.
We start at 20 and step numbers by 20, as Michael Mior suggested. We use a generator expression inside of all(), as poke suggested.
Instead of a while loop, I used a for loop with xrange(); I think this is slightly faster.
The result:
check_list = [11, 13, 14, 16, 17, 18, 19, 20]
def find_solution(step):
for num in xrange(step, 999999999, step):
if all(num % n == 0 for n in check_list):
return num
return None
if __name__ == '__main__':
solution = find_solution(20)
if solution is None:
print "No answer found"
else:
print "found an answer:", solution
On my computer, this finds an answer in under nine seconds.
EDIT:
And, if we take advice from David Zaslavsky, we realize we can start the loop at 2520, and step by 2520. If I do that, then on my computer I get the correct answer in about a tenth of a second.
I made find_solution() take an argument. Try calling find_solution(2520).
My first answer sped up the original calculation from the question.
Here's another answer that solves it a different way: just find all the prime factors of each number, then multiply them together to go straight to the answer. In other words, this automates the process recommended by poke in a comment.
It finishes in a fraction of a second. I don't think there is a faster way to do this.
I did a Google search on "find prime factors Python" and found this:
http://www.stealthcopter.com/blog/2009/11/python-factors-of-a-number/
From that I found a link to factor.py (written by Mike Hansen) with some useful functions:
https://gist.github.com/weakish/986782#file-factor-py
His functions didn't do quite what I wanted, so I wrote a new one but used his pull_prime_factors() to do the hard work. The result was find_prime_factors() which returns a list of tuples: a prime number, and a count. For example, find_prime_factors(400) returns [(2,4), (5,2)] because the prime factors of 400 are: (2*2*2*2)*(5*5)
Then I use a simple defaultdict() to keep track of how many we have seen so far of each prime factor.
Finally, a loop multiplies everything together.
from collections import defaultdict
from factor import pull_off_factors
pf = defaultdict(int)
_primes = [2,3,5,7,11,13,17,19,23,29]
def find_prime_factors(n):
lst = []
for p in _primes:
n = pull_off_factors(n, p, lst)
return lst
def find_solution(low, high):
for num in xrange(low, high+1):
lst = find_prime_factors(num)
for n, count in lst:
pf[n] = max(pf[n], count)
print "prime factors:", pf
solution = 1
for n, count in pf.items():
solution *= n**count
return solution
if __name__ == '__main__':
solution = find_solution(1, 20)
print "answer:", solution
EDIT: Oh wow, I just took a look at #J.F. Sebastian's answer to a related question. His answer does essentially the same thing as the above code, only far more simply and elegantly. And it is in fact faster than the above code.
Least common multiple for 3 or more numbers
I'll leave the above up, because I think the functions might have other uses in Project Euler. But here's the J.F. Sebastian solution:
def gcd(a, b):
"""Return greatest common divisor using Euclid's Algorithm."""
while b:
a, b = b, a % b
return a
def lcm(a, b):
"""Return lowest common multiple."""
return a * b // gcd(a, b)
def lcmm(*args):
"""Return lcm of args."""
return reduce(lcm, args)
def lcm_seq(seq):
"""Return lcm of sequence."""
return reduce(lcm, seq)
solution = lcm_seq(xrange(1,21))
print "lcm_seq():", solution
I added lcm_seq() but you could also call:
lcmm(*range(1, 21))
Since your answer must be divisible by 20, you can start at 20 and increment by 20 instead of by two. In general, you can start at rangemax and increment by rangemax. This reduces the number of times div_check is called by an order of magnitude.
Break down the number as a prime factorization.
All primes less than 20 are:
2,3,5,7,11,13,17,19
So the bare minimum number that can be divided by these numbers is:
2*3*5*7*11*13*17*19
Composites:
4,6,8,9,10,12,14,15,16,18,20 = 2^2, 2*3, 2^3, 3^2, 2*5, 2^2*3, 2*7, 3*5, 2*3^2, 2^2*5
Starting from the left to see which factors needed:
2^3 to build 4, 8, and 16
3 to build 9
Prime factorization: 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19 = 232,792,560
I got the solution in 0.066 milliseconds (only 74 spins through a loop) using the following procedure:
Start with smallest multiple for 1, which = 1. Then find the smallest multiple for the next_number_up. Do this by adding the previous smallest multiple to itself (smallest_multiple = smallest_multiple + prev_prod) until next_number_up % smallest_multiple == 0. At this point smallest_multiple is the correct smallest multiple for next_number_up. Then increment next_number_up and repeat until you reach the desired smallest_multiple (in this case 20 times). I believe this finds the solution in roughly n*log(n) time (though, given the way numbers seem to work, it seems to complete much faster than that usually).
For example:
1 is the smallest multiple for 1
Find smallest multiple for 2
Check if previous smallest multiple works 1/2 = .5, so no
previous smallest multiple + previous smallest multiple == 2.
Check if 2 is divisible by 2 - yes, so 2 is the smallest multiple for 2
Find smallest multiple for 3
Check if previous smallest multiple works 2/3 = .667, so no
previous smallest multiple + previous smallest multiple == 4
Check if 4 is divisible by 3 - no
4 + previous smallest multiple == 6
Check if 6 is divisible by 3 - yes, so 6 is the smallest multiple for 3
Find smallest multiple for 4
Check if previous smallest multiple works 6/4 = 1.5, so no
previous smallest multiple + previous smallest multiple == 12
Check if 12 is divisble by 4 - yes, so 12 is the smallest multiple for 4
repeat until 20..
Below is code in ruby implementing this approach:
def smallestMultiple(top)
prod = 1
counter = 0
top.times do
counter += 1
prevprod = prod
while prod % counter != 0
prod = prod + prevprod
end
end
return prod
end
List comprehensions are faster than for loops.
Do something like this to check a number:
def get_divs(n):
divs = [x for x in range(1,20) if n % x == 0]
return divs
You can then check the length of the divs array to see if all the numbers are present.
Two different types of solutions have been posted here. One type uses gcd calculations; the other uses prime factorization. I'll propose a third type, which is based on the prime factorization approach, but is likely to be much faster than prime factorization itself. It relies on a few simple observations about prime powers -- prime numbers raised to some integral exponent. In short, it turns out that the least common multiple of all numbers below some number n is equal to the product of all maximal prime powers below n.
To prove this, we begin by thinking about the properties that x, the least common multiple of all numbers below n, must have, and expressing them in terms of prime powers.
x must be a multiple of all prime powers below n. This is obvious; say n = 20. 2, 2 * 2, 2 * 2 * 2, and 2 * 2 * 2 * 2 are all below 20, so they all must divide x. Likewise, 3 and 3 * 3 are both below n and so both must divide x.
If some number a is a multiple of the prime power p ** e, and p ** e is the maximal power of p below n, then a is also a multiple of all smaller prime powers of p. This is also quite obvious; if a == p * p * p, then a == (p * p) * p.
By the unique factorization theorem, any number m can be expressed as a multiple of prime powers less than m. If m is less than n, then m can be expressed as a multiple of prime powers less than n.
Taken together, the second two observations show that any number x that is a multiple of all maximal prime powers below n must be a common multiple of all numbers below n. By (2), if x is a multiple of all maximal prime powers below n, it is also a multiple of all prime powers below n. So by (3), it is also a multiple of all other numbers below n, since they can all be expressed as multiples of prime powers below n.
Finally, given (1), we can prove that x is also the least common multiple of all numbers below n, because any number less than x could not be a multiple of all maximal prime powers below n, and so could not satisfy (1).
The upshot of all this is that we don't need to factorize anything. We can just generate primes less than n!
Given a nicely optimized sieve of eratosthenes, one can do that very quickly for n below one million. Then all you have to do is find the maximal prime power below n for each prime, and multiply them together.
prime_powers = [get_max_prime_power(p, n) for p in sieve(n)]
result = reduce(operator.mul, prime_powers)
I'll leave writing get_max_prime_power as an exercise. A fast version, combined with the above, can generate the lcm of all numbers below 200000 in 3 seconds on my machine.
The result is a 86871-digit number!
This solution ran pretty quickly for me (imports numpy).
t0 = time.time()
import numpy
ints = numpy.array(range(1,21))
primes = [2,3,5,7,11,13,17,19] # under 20
facts = []
for p in primes:
counter = 0
nums = ints
while any(nums % p == 0):
nums = nums / float(p)
counter += 1
facts.append(counter)
facts = numpy.array(facts)
mults = primes**facts
ans = 1
for m in mults:
ans = m * ans
t1 =time.time()
perf = t1 - t0
print "Problem 5\nAnswer:",ans, "runtime:", perf, "seconds"
"""Problem 5
Answer: 232792560 runtime: 0.00505399703979 seconds"""
Here i have also done using prime factorization way.
#!/usr/bin/env python
import math
def is_prime(num):
if num > 1:
if num == 2:
return True
if num%2 == 0:
return False
for i in range(3, int(math.sqrt(num))+1, 2):
if num%i == 0:
return False
return True
return False
def lcm(number):
prime = []
lcm_value = 1
for i in range(2,number+1):
if is_prime(i):
prime.append(i)
final_value = []
for i in prime:
x = 1
while i**x < number:
x = x + 1
final_value.append(i**(x-1))
for j in final_value:
lcm_value = j * lcm_value
return lcm_value
if __name__ == '__main__':
print lcm(20)
After checking how much time it has taken, it was not bad at all.
root#l-g6z6152:~/learn/project_euler# time python lcm.py
232792560
real 0m0.019s
user 0m0.008s
sys 0m0.004s
I wrote a solution to euler5 that:
Is orders of magnitude faster than most of the solutions here when n=20 (though not all respondents report their time) because it uses no imports (other than to measure time for this answer) and only basic data structures in python.
Scales much better than most other solutions. It will give the answer for n=20 in 6e-05 seconds, or for n=100 in 1 millisec, faster than most of the responses for n=20 listed here.
import time
a=time.clock() # set timer
j=1
factorlist=[]
mydict={}
# change second number to desired number +1 if the question were changed.
for i in range(2,21,1):
numberfactors=[]
num=i
j=2
# build a list of the prime factors
for j in range(j,num+1,1):
counter=0
if i%j==0:
while i%j==0:
counter+=1
numberfactors.append(j)
i=i/j
# add a list of factors to a dictionary, with each prime factor as a key
if j not in mydict:
mydict[j] = counter
# now, if a factor is already present n times, including n times the factor
# won't increase the LCM. So replace the dictionary key with the max number of
# unique factors if and only if the number of times it appears is greater than
# the number of times it has already appeared.
# for example, the prime factors of 8 are 2,2, and 2. This would be replaced
# in the dictionary once 16 were found (prime factors 2,2,2, and 2).
elif mydict[j] < counter:
mydict[j]=counter
total=1
for key, value in mydict.iteritems():
key=int(key)
value=int(value)
total=total*(key**value)
b=time.clock()
elapsed_time=b-a
print total, "calculated in", elapsed_time, "seconds"
returns:
232792560 calculated in 6e-05 seconds
# does not rely on heuristics unknown to all users, for instance the idea that
# we only need to include numbers above 10, etc.
# For all numbers evenly divisible by 1 through 100:
69720375229712477164533808935312303556800 calculated in 0.001335 seconds
Here is program in C language. Cheers
#include <stdio.h>
#include <stdlib.h>
//2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
bez_ost(int q)
{
register br=0;
for( register i=1;i<=20;i++)
if(q%i==0)
br++;
if(br==20)
return 1;
return 0;
}
int main()
{
register j=20;
register ind=0;
while(ind!=1)
{
j++;
if(bez_ost(j))
break;
}
fprintf(stdout,"\nSmallest positive number that is evenlu divisible by all of the numbers from 1 to 20 is: %d\n\a",j);
system("Pause");
}
I've had the same problem. The algorithm seems to be quite slow, but it does work nonetheless.
result = list()
xyz = [x for x in range(11, 21)]
number = [2520]
count = 0
while len(result) == 0:
for n in number:
print n
for x in xyz:
if n % x == 0:
count += 1
elif n % x != 0:
count = 0
break
if count == 10:
result.append(number[0])
elif count != 10:
number[0] += 1
print result
This was the algorithm I made.
How about this? The required number is, after all, the LCM of the given numbers.
def lcm(a,b):
lcm1 = 0
if a == b:
lcm1 = a
else:
if a > b:
greater = a
else:
greater = b
while True:
if greater % a == 0 and greater % b == 0:
lcm1 = greater
break
greater += 1
return lcm1
import time
start_time = time.time()
list_numbers = list(range(2,21))
lcm1 = lcm(list_numbers[0],list_numbers[1])
for i in range(2,len(list_numbers)):
lcm1 = lcm(lcm1,list_numbers[i])
print(lcm1)
print('%0.5f'%(time.time()-start_time))
This code took a full 45 s to get the answer to the actual question! Hope it helps.
import time
primes = [11,13,17,19]
composites = [12,14,15,16,18,20]
def evenlyDivisible(target):
evenly = True
for n in composites:
if target % n > 0:
evenly = False
break
return evenly
step = 1
for p in primes:
step *= p
end = False
number = 0
t1 = time.time()
while not end:
number += step
if evenlyDivisible(number):
end = True
print("Smallest positive evenly divisible number is",number)
t2 = time.time()
print("Time taken =",t2-t1)
Executed in 0.06 seconds
Here is my Python solution, it has 12 iteration so compiled quite fast:
smallest_num = 1
for i in range (1,21):
if smallest_num % i > 0: # If the number is not divisible by i
for k in range (1,21):
if (smallest_num * k) % i == 0: # Find the smallest number divisible by i
smallest_num = smallest_num * k
break
print (smallest_num)
Here's an observation on this problem. Ultimately, it takes 48 iterations to find the solution.
Any number that is divisible by all of the numbers from 1..20 must be divisible by the product of the primes in that range, namely 2, 3, 5, 7, 11, 13, 17, and 19. It cannot be smaller than the product of these primes, so let's use that number, 232,792,560, as the increment, rather than 20, or 2,520, or some other number.
As it turns out, 48 * 232,792,560 is divisible by all numbers 1..20. By the way, the product of all of the non-primes between 1..20 is 66. I haven't quite figured out the relationship between 48 and 66 in this context.
up = int(input('Upper limit: '))
number = list(range(1, up + 1))
n = 1
for i in range(1, up):
n = n * number[i]
for j in range(i):
if number[i] % number[j] == 0:
n = n / number[j]
number[i] = number[i] / number[j]
print(n)
How I can reduce the complexity of this
num = 1
found = False
while not found:
count =0
for i in range(1, 21):
if num %i == 0:
count+=1
if count ==10:
print(num)
found = True
num+=1
Here is the code in C++ to find the solution for this question.
What we have to do is to run a loop from 1 until we got that number so just iterate through the loop and once the number get evenly divisble(remainder 0) flag value dont get change and flag remains 1 and we got that number and break through outer loop and print the answer
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i,j,flag=1;
for(i=1;;i++) //iterate until we got the number
{
flag=1;
for(j=2;j<=20;j++) //check form 1 to 20 for that number
{
if(i%j!=0) //if the number is not evenly divisible we break loop and
{
flag=0;break; // initilize flag as 0 i.e. that number is not what we want
}
}
if(flag==1) //if any number we got that is evenly divisible i.e. flag value doesnt change we got that number we break through the loop and print the answer
break;
} // after ending of the loop as we jump to next number make flag also 1 again so that again inner loop conditions apply on it
cout<<i;
return 0;
}
A typescript variant that seems to be relatively quick, leveraging recursion and known facts.
describe(`2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?`,
() => {
it("prooves the example: 10", () => smallestWithoutRemainder(10).should.be.equal(2520));
it("prooves 1", () => smallestWithoutRemainder(1).should.be.equal(1));
it("prooves 2", () => smallestWithoutRemainder(2).should.be.equal(2));
it("prooves 3", () => smallestWithoutRemainder(3).should.be.equal(6));
it("prooves 4", () => smallestWithoutRemainder(4).should.be.equal(12));
it("prooves 5", () => smallestWithoutRemainder(5).should.be.equal(60));
it("prooves 6", () => smallestWithoutRemainder(6).should.be.equal(60));
it("prooves 7", () => smallestWithoutRemainder(7).should.be.equal(420));
it("prooves 8", () => smallestWithoutRemainder(8).should.be.equal(840));
it("prooves 9", () => smallestWithoutRemainder(9).should.be.equal(2520));
it("prooves 12", () => smallestWithoutRemainder(12).should.be.equal(27720));
it("prooves 20", () => smallestWithoutRemainder(20).should.be.equal(232792560));
it("prooves 30", () => smallestWithoutRemainder(30).should.be.equal(2329089562800));
it("prooves 40", () => smallestWithoutRemainder(40).should.be.equal(5342931457063200));
});
let smallestWithoutRemainder = (end: number, interval?: number) => {
// What do we know?
// - at 10, the answer is 2520
// - can't be smaller than the lower multiple of 10
// - must be an interval of the lower multiple of 10
// so:
// - the interval and the start should at least be divisable by 'end'
// - we can recurse and build on the results before it.
if (!interval) interval = end;
let count = Math.floor(end / 10);
if (count == 1) interval = 2520;
else if (count > 1) interval = smallestWithoutRemainder((count - 1) * 10, interval);
for (let i = interval; true; i += interval) {
let failed = false;
for (let j = end; j > 1; j--) {
if (i % j != 0) {
failed = true;
break;
}
}
if (!failed) return i;
}
}
I think this the answer:
primes = [11, 13, 17, 19]
result = 2520
for i in primes:
result *= i
print (result * 2)
I have a very big number, and I want to make a program, that finds two prime numbers, that will give the original number, if multiplied.
Ex.
Original_number = 299
// The program should get these two numbers:
q = 13
p = 23
The program runs fine at the start, but at a certain point, it just stops, and I'm not sure what is wrong.
The code:
import time
import math
def main():
time1 = time.clock()
q = int(0)
p = int(0)
finalnumber = int(377)
print("in main \n")
print("q = ", q)
print("\n p = ", p)
gotResult = 0
while(gotResult == 0):
p = GetNextPrime(p)
if(p >= finalnumber):
q = GetNextPrime(q)
p = q
p = GetNextPrime(p)
if(q * p == finalnumber):
gotResult == 1
break
print("q = ", q)
print("\n p = ", p)
time2 = time.clock()
ElapsedTime = time2 - time1
print("Elapsed time: ", ElapsedTime)
def GetNextPrime(prime):
print("in GetNextPrime \n")
isPrime = 0
while(isPrime == 0):
prime = prime + 1
if(IsPrime(prime)== 1):
isPrime = 1
return prime
def IsPrime(prime):
print("in IsPrime \n")
isPrime = 0
i = 2
while(i <= math.sqrt(prime)):
if(i % 2 == 0):
i = i+1
if(prime % i == 0):
isPrime = 1
break
return isPrime
#start of program here
main()
I have written the program in python, and I know it probably isn't good, because I'm new to python.(I have been programming C++, and I'm not even good at it)
But I hope you can help me find the problem :)
ps. What is the maximum size of the original number? How many ciphers can it have?
Just factorize a number. You'll get a list of prime factors. If the list contains exactly two numbers, and the numbers are good for your purposes, you won. Else try another number.
But the approach above is quite wasteful. I'd rather take a list of primes, generate all pairs of it and multiply. The result would be a list of numbers that, well, can only be factorized into 2 primes. Like this:
some_primes = [2, 3, 5, 7, 11] # you can generate a better list
my_numbers = [x*y for x in some_primes for y in some_primes]
A simple approach is trial division:
import math
def factors(number):
return [(x, number / x) for x in range(int(math.sqrt(number)))[2:] if not number % x]
Then factors(299) returns [(13,23)]
There are problems with this method for large numbers:
Large numbers may exceed the python integer limit (found in sys.maxint). A 64-bit machine will be limited to 18 decimal digits.
Factoring large numbers is a hard problem, and an open research question. Trial division is about as brute force as it comes, but it will work for smaller numbers. Otherwise, you'll quickly need a more sophisticated algorithm. See wikipedia for a discussion.
If you're going to brute-force numerical problems, python is the wrong language. Identical algorithms will run faster in a compiled language like C++.
isPrime is wrong. You return 1 when the number is not prime. Also you never test if the number can be divided by 2. I didn't look any further than that.
Protip: Python is not C. There is True, False and you don't need all the brackets in if, while.
You should really test every function you write, not the whole program - that tells you nothing about where the bugs are.
In addition to Jochel Ritzel's and DSM's answers, the logic in main() while loop fails to account for cases when the number is not a product of two primes (then it will go into infinite loop).
Also, if you expect to factor really large numbers (say more than 20-30 digits), your approach is probably too slow. You should use Erastothenes sieve at minimum to generate a large enough list of primes in advance if you want to get acceptable results.
There are (pretty sophisticated) algoritms to deal with larger cases, but in general, this is a difficult problem and the solution to it scales very badly with number of digits.
In the following logic:
while(i <= math.sqrt(prime)):
if(i % 2 == 0):
i = i+1
if(prime % i == 0):
isPrime = 1
break
If i is odd and prime isn't divisible by it, it'll loop forever, and here it gets stuck on 3. [The other obvious problem has already been pointed out.]