Reputation: 1157
I am unsure what is wrong with my code. I am trying to write a program that finds the prime factorization of a number, and iterates through numbers. My code is
import math
import time
def primfacfind(n1,n2):
while n1 < n2:
n = n1
primfac=[]
time_start = time.clock()
def primes(n):
sieve = [True] * n
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
return [2] + [i for i in xrange(3,n,2) if sieve[i]]
print primes(n)
def factfind(lsp,n): #finds factors of n among primes
for i in lsp:
if n%i==0:
primfac.append(i)
else:
i+=1
factfind(primes(n),n)
print primfac
def simplify(lsp):
for i in lsp:
if lsp.count(i) > 1:
i+=1 #should simplify to 3^2 instead of 3,3; unfinished
time_end = time.clock()
time_elapsed = time_end - time_start
print time_elapsed
n1+=1
print primfacfind(6,15)
The error given is
Traceback (most recent call last):
File "python", line 15
sieve = [True] * n
^
IndentationError: expected an indented block
I've checked over my indentation again and again, and I am unsure as to what is wrong. The program worked when it was not in the overall function and while loop, but I don't see how that should make a difference. It would be appreciated if the answer code was as simple as possible to understand, as I am somewhat new to python.
Any help with this error would be appreciated. Thanks!
Upvotes: 0
Views: 161
Reputation: 371
Download something like Sublime and highlight the code. Spaces will be dots and tabs will be dashes.
Upvotes: 2
Reputation: 29
I put that code in my editor and it compiled just fine. So I went to line 12 where you have sieve = [True] * n
and got rid of the indentation so it was indented the same as the line above it def primes(n):
and I was able to recreate your error.
Perhaps try and add an additional indentation than you think. You can also try Enthought Canopy which is free if you go to a university if you want a different editor.
Upvotes: -1