Have a problem which asks to find the sum of the positive even numbers and negative odd numbers, 1 to 100 (So 1+2-3+4....+98-99+100). Here is what I have done so far and the correct sum should be 52 if I am doing my math correctly but I come out with a sum of 50. Any suggestions?
lst = range(1,101)
>>> total = 0
>>> for x in lst:
... if x % 2:
... total -= x
... else:
... total += x
...
>>> total
50
I believe your code is right and your math is wrong. Here are three ways to solve the problem.
your solution:
lst = range(1,101)
total = 0
for x in lst:
if x % 2:
total -= x
else:
total += x
print(total)
50
Sum of even numbers plus the sum of odd numbers:
def sumForLoop(max):
positiveEven = sum(range(2,max+1,2))
negativeOdd = -sum(range(1,max+1, 2))
print(positiveEven + negativeOdd)
sumForLoop(100)
50
A formula for the total:
def sumFormula(max):
print(-1**100 *math.floor(max/2))
sumFormula(100)
50
I am trying to write a function in python that returns a list of all the fibonacci numbers in a certain range but my code wont work it simply returns [0]. What is the problem?
from math import sqrt
def F(n):
return int(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)))
def Frange(x):
A = [0]
while max(A) < x:
H = 1
for i in range(H):
A.append(F(i))
H = H+1
return A
You set H = 1 as the first statement in your while loop; so every time you enter the for loop, H = 1 and you'll only get the Fibonacci number for n=0
You need to set H = 1 outside the while loop:
def Frange(x):
A = [0]
H = 1
while max(A) < x:
for i in range(H):
A.append(F(i))
H = H+1
return A
You could have solved this yourself very easily by printing various values inside the loops, such as print H.
I found another error and the improved code is:
from math import sqrt
def F(n):
return int(((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5)))
def Frange2(x):
A = [0]
H = 1
while max(A) < x:
if F(H) < x:
A.append(F(H))
else:
break
H = H+1
return A
The fastest and most popular and uncomplicated solution to calculating a list of fibonacci numbers in a range is
def fib3(n): #FASTEST YET
fibs= [0,1] #list from bottom up
for i in range(2, n+1):
fibs.append(fibs[-1]+fibs[-2])
return fibs
This function stores the computed fibonacci numbers in a list and later uses them as 'cached' numbers to compute further.
Hope it helps!
You might have guessed that I'm doing project euler #12 by the title. My brute force solution took much too long, so I went looking for optimizations that I could understand.
I'm interested in extending the strategy outlined here
The way I've tried to tackle this is by using the Sieve of Eratosthenes to get prime factors like this:
divs = []
multiples = set()
for i in xrange(2, n + 1):
if i not in multiples:
if n % i == 0:
divs.append(i)
multiples.update(xrange(2*i, n+1, i))
return divs
This itself is a problem because line 8 will yield an overflow error long before the program gets within the range of the answer (76576500).
Now, assuming I'm able to get the prime factors, how can I find their respective multiplicities efficiently?
Borrowing from the other answer:
The number a1^k1*a2^k2*...an^kn has number of factor = (k1+1)*(k2+1)...(kn+1)
You can get the prime numbers below a certain number using the following code:
Courtesy of Fastest way to list all primes below N
n = number
def primesfrom2to(n):
""" Input n>=6, Returns a array of primes, 2 <= p < n """
sieve = numpy.ones(n/3 + (n%6==2), dtype=numpy.bool)
for i in xrange(1,int(n**0.5)/3+1):
if sieve[i]:
k=3*i+1|1
sieve[ k*k/3 ::2*k] = False
sieve[k*(k-2*(i&1)+4)/3::2*k] = False
return numpy.r_[2,3,((3*numpy.nonzero(sieve)[0][1:]+1)|1)]
primes = primesfrom2to(n).tolist() # list of primes.
primes = map(int, primes)
factors = {}
for prime in primes:
n = number
factor = 0
while True:
if n%prime == 0:
factor += 1
n /= prime
factors[prime] = factor
else: break
factors will give you the multiplicity of the prime factors.
My standard prime-numbers script is appended below; it provides the Sieve of Eratsothenes to generate primes, a Miller-Rabin primality test, a function that factors integers using a 2,3,5-wheel and Pollard's rho method, the number-theoretic function sigma that calculates the sum of the x'th powers of the divisors of an integer, using the method that you reference in your post, and a function that computes the aliquot sequence starting from a given integer. Given that script, it is easy to solve Project Euler 12, remembering that sigma with x=0 returns the count of the divisors of an integer:
$ python
Python 2.6.8 (unknown, Jun 9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile('primes.py')
>>> factors(76576500)
[2, 2, 3, 3, 5, 5, 5, 7, 11, 13, 17]
>>> sigma(0,76576500)
576
>>> i, t = 1, 1
>>> while sigma(0, t) < 500:
... i += 1; t += i
...
>>> print t
76576500
You can run the program at http://programmingpraxis.codepad.org/V5LiI8V9, and you'll find lots of prime-number stuff at my blog. Here's the code:
# prime numbers
def primes(n): # sieve of eratosthenes
i, p, ps, m = 0, 3, [2], n // 2
sieve = [True] * m
while p <= n:
if sieve[i]:
ps.append(p)
for j in range((p*p-3)/2, m, p):
sieve[j] = False
i, p = i+1, p+2
return ps
# from random import randint
seed = 17500728 # RIP j s bach
def random(): # float on range [0,1)
global seed
seed = (69069 * seed + 1234567) % 4294967296
return seed / 4294967296.0
def randint(lo,hi): # int on range [lo,hi)
return int((hi - lo) * random()) + lo
def isPrime(n, k=5): # miller-rabin
if n < 2: return False
for p in [2,3,5,7,11,13,17,19,23,29]:
if n % p == 0: return n == p
s, d = 0, n-1
while d % 2 == 0:
s, d = s+1, d/2
for i in range(k):
x = pow(randint(2, n-1), d, n)
if x == 1 or x == n-1: continue
for r in range(1, s):
x = (x * x) % n
if x == 1: return False
if x == n-1: break
else: return False
return True
# from fractions import gcd
def gcd(a,b): # greatest common divisor
if b == 0: return a
return gcd(b, a % b)
def insertSorted(x, xs): # insert x in order
i, ln = 0, len(xs)
while i < ln and xs[i] < x: i += 1
xs.insert(i,x)
return xs
def factors(n, b2=-1, b1=10000): # 2,3,5-wheel, then rho
if -1 <= n <= 1: return [n]
if n < -1: return [-1] + factors(-n)
wheel = [1,2,2,4,2,4,2,4,6,2,6]
w, f, fs = 0, 2, []
while f*f <= n and f < b1:
while n % f == 0:
fs.append(f)
n /= f
f, w = f + wheel[w], w+1
if w == 11: w = 3
if n == 1: return fs
h, t, g, c = 1, 1, 1, 1
while not isPrime(n):
while b2 <> 0 and g == 1:
h = (h*h+c)%n # the hare runs
h = (h*h+c)%n # twice as fast
t = (t*t+c)%n # as the tortoise
g = gcd(t-h, n); b2 -= 1
if b2 == 0: return fs
if isPrime(g):
while n % g == 0:
fs = insertSorted(g, fs)
n /= g
h, t, g, c = 1, 1, 1, c+1
return insertSorted(n, fs)
def sigma(x, n, fs=[]): # sum of x'th powers of divisors of n
def add(s, p, m):
if x == 0: return s * (m+1)
return s * (p**(x*(m+1))-1) / (p**x-1)
if fs == []: fs = factors(n)
prev, mult, sum = fs.pop(0), 1, 1
while len(fs) > 0:
fact = fs.pop(0)
if fact <> prev:
sum, prev, mult = add(sum, prev, mult), fact, 1
else: mult += 1
return add(sum, prev, mult)
def aliquot(n): # print aliquot sequence
s, ss, k, fs = n, [n], 0, factors(n)
print n, k, s, fs
while s > 1:
s, k = sigma(1,s,fs) - s, k + 1
fs = factors(s)
print n, k, s, fs
if s in ss: return "cycle"
ss.append(s)
return ss.pop(-2)
Your approach for factorization is far from optimal, even if you limit yourself to relatively simple algorithms (i.e. not Brent's algorithm or anything more advanced).
Each time you find a prime factor, divide by that factor until it is no longer divisible. The number of times you can do that is the multiplicity.
Continue with the quotient after division, not your original number.
Find factors and divide until the remaining quotient is less than the square of your divisor. In that case the quotient is 1 or a prime (the last prime factor with multiplicity 1).
To find the factor it is enough to do trial division by 2 and odd numbers starting from 3. Any non-primes will not be a problem, because its prime factors will already be removed before it is reached.
Use the correct data structure to represent the prime factors together with their multiplicity (a map or multiset).
You can also compute the number of divisors directly, without storing the factorization. Each time you find a prime factor and its multiplicity, you can accumulate the result by multiplying with the corresponding factor from the formula for the number of divisors.
If you need to do many factorizations of numbers that are not too big, you can precompute an array with the smallest divisor for each array index, and use that to quickly find divisors.
example: 28 = 2^2 * 7 ^ 1
number of factors = (2 + 1) * (1 + 1) = 6
in general a ^ k1 * a ^ k2 .. a ^ kn
number of factors = (k1 + 1) * (k2 + 1) ... (kn + 1)
I'm attempting to enumerate all possible matrices of size r by r with a few constraints.
Row and column sums must be in non-ascending order.
Starting from the top left element down the main diagonal, each row and column subset from that entry must be made up of combinations with replacements from 0 to the value in that upper left entry (inclusive).
The row and column sums must all be less than or equal to a predetermined n value.
The main diagonal must be in non-ascending order.
Important note is that I need every combination to be store somewhere, or if written in c++, to be ran through another few functions after finding them
r and n are values that range from 2 to say 100.
I've tried a recursive way to do this, along with an iterative, but keep getting hung up on keeping track column and row sums, along with all the data in a manageable sense.
I have attached my most recent attempt (which is far from completed), but may give you an idea of what is going on.
The function first_section(): builds row zero and column zero correctly, but other than that I don't have anything successful.
I need more than a push to get this going, the logic is a pain in the butt, and is swallowing me whole. I need to have this written in either python or C++.
import numpy as np
from itertools import combinations_with_replacement
global r
global n
r = 4
n = 8
global myarray
myarray = np.zeros((r,r))
global arraysums
arraysums = np.zeros((r,2))
def first_section():
bigData = []
myarray = np.zeros((r,r))
arraysums = np.zeros((r,2))
for i in reversed(range(1,n+1)):
myarray[0,0] = i
stuff = []
stuff = list(combinations_with_replacement(range(i),r-1))
for j in range(len(stuff)):
myarray[0,1:] = list(reversed(stuff[j]))
arraysums[0,0] = sum(myarray[0,:])
for k in range(len(stuff)):
myarray[1:,0] = list(reversed(stuff[k]))
arraysums[0,1] = sum(myarray[:,0])
if arraysums.max() > n:
break
bigData.append(np.hstack((myarray[0,:],myarray[1:,0])))
if printing: print 'myarray \n%s' %(myarray)
return bigData
def one_more_section(bigData,index):
newData = []
for item in bigData:
if printing: print 'item = %s' %(item)
upperbound = int(item[index-1]) # will need to have logic worked out
if printing: print 'upperbound = %s' % (upperbound)
for i in reversed(range(1,upperbound+1)):
myarray[index,index] = i
stuff = []
stuff = list(combinations_with_replacement(range(i),r-1))
for j in range(len(stuff)):
myarray[index,index+1:] = list(reversed(stuff[j]))
arraysums[index,0] = sum(myarray[index,:])
for k in range(len(stuff)):
myarray[index+1:,index] = list(reversed(stuff[k]))
arraysums[index,1] = sum(myarray[:,index])
if arraysums.max() > n:
break
if printing: print 'index = %s' %(index)
newData.append(np.hstack((myarray[index,index:],myarray[index+1:,index])))
if printing: print 'myarray \n%s' %(myarray)
return newData
bigData = first_section()
bigData = one_more_section(bigData,1)
A possible matrix could look like this:
r = 4, n >= 6
|3 2 0 0| = 5
|3 2 0 0| = 5
|0 0 2 1| = 3
|0 0 0 1| = 1
6 4 2 2
Here's a solution in numpy and python 2.7. Note that all the rows and columns are in non-increasing order, because you only specified that they should be combinations with replacement, and not their sortedness (and generating combinations is the simplest with sorted lists).
The code could be optimized somewhat by keeping row and column sums around as arguments instead of recomputing them.
import numpy as np
r = 2 #matrix dimension
maxs = 5 #maximum sum of row/column
def generate(r, maxs):
# We create an extra row and column for the starting "dummy" values.
# Filling in the matrix becomes much simpler when we do not have to treat cells with
# one or two zero indices in special way. Thus, we start iteration from the
# (1, 1) index.
m = np.zeros((r + 1, r + 1), dtype = np.int32)
m[0] = m[:,0] = maxs + 1
def go(n, i, j):
# If we completely filled the matrix, yield a copy of the non-dummy parts.
if (i, j) == (r, r):
yield m[1:, 1:].copy()
return
# We compute the next indices in row major order (the choice is arbitrary).
(i2, j2) = (i + 1, 1) if j == r else (i, j + 1)
# Computing the maximum possible value for the current cell.
max_val = min(
maxs - m[i, 1:].sum(),
maxs - m[1:, j].sum(),
m[i, j-1],
m[i-1, j])
for n2 in xrange(max_val, -1, -1):
m[i, j] = n2
for matrix in go(n2, i2, j2):
yield matrix
return go(maxs, 1, 1) #note that this is a generator object
# testing
for matrix in generate(r, maxs):
print
print matrix
If you'd like to have all the valid permutations in the rows and columns, this code below should work.
def generate(r, maxs):
m = np.zeros((r + 1, r + 1), dtype = np.int32)
rows = [0]*(r+1) # We avoid recomputing row/col sums on each cell.
cols = [0]*(r+1)
rows[0] = cols[0] = m[0, 0] = maxs
def go(i, j):
if (i, j) == (r, r):
yield m[1:, 1:].copy()
return
(i2, j2) = (i + 1, 1) if j == r else (i, j + 1)
max_val = min(rows[i-1] - rows[i], cols[j-1] - cols[j])
if i == j:
max_val = min(max_val, m[i-1, j-1])
if (i, j) != (1, 1):
max_val = min(max_val, m[1, 1])
for n in xrange(max_val, -1, -1):
m[i, j] = n
rows[i] += n
cols[j] += n
for matrix in go(i2, j2):
yield matrix
rows[i] -= n
cols[j] -= n
return go(1, 1)
I'm struggling with Euler Problem 12. I do not want the answer or exact code changes that'll get me there. I'm just looking to be pointed in the right direction. I ran this code for about 10 minutes and came up with an answer which was incorrect. This leads me to believe that my assumption that a triangle number with > 500 divisors would not have any factors > 10000 is incorrect. I'm thinking I need to use a faster prime generator AND get the program to stop iterating through lists so much. I am not sure how to do the latter.
def eratosthenes_sieve(limit):
primes = {}
listofprimes = []
for i in range(2, limit + 1):
primes[i] = True
for i in primes:
factors = range(i, limit + 1, i)
for f in factors[1:limit + 1]:
primes[f] = False
for i in primes:
if primes[i] == True:
listofprimes.append(i)
return listofprimes
def prime_factorization(n):
global primal
prime_factors = {}
for i in primal:
if n < i:
i = primal[0]
if n % i == 0:
if i not in prime_factors.keys():
prime_factors[i] = 1
else:
prime_factors[i] += 1
n = n / i
if n in primal:
if n not in prime_factors.keys():
prime_factors[n] = 1
else:
prime_factors[n] += 1
return prime_factors
return prime_factors
def divisor_function(input):
x = 1
for exp in input.values():
x *= exp + 1
return x
def triangle(th):
terms = []
for each in range(1, th+1):
terms.append(each)
return sum(terms)
z = 1
primal = eratosthenes_sieve(10000)
found = False
while found == False:
triz = triangle(z)
number_of_divisors = divisor_function(prime_factorization(triz))
if number_of_divisors > 300:
print "GETTING CLOSE!! ********************************"
if number_of_divisors > 400:
print "SUPER DUPER CLOSE!!! *********************************************************"
if number_of_divisors < 501:
print "Nope. Not %s...Only has %s divisors." % (triz, number_of_divisors)
z += 1
else:
found = True
print "We found it!"
print "The first triangle number with over 500 divisors is %s!" % triangle(z)
Of course I figure it out minutes after posting.
In my prime_factorization function.
if n % i == 0: should have been while n % i == 0.
That was causing the program to miss factors and run through the entire list of primes.