Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 17 Online
OpenStudy (anonymous):

what do you guys thin about for this answer for hw1 problem 1

OpenStudy (anonymous):

def isprime(startnumber): startnumber*=1.0 for divisor in range(2,int(startnumber/2)+1): if startnumber/divisor==int(startnumber/divisor): return False return True number=0 prime_counter=0 max_count=9001 while(prime_counter<max_count): number=number+1 prime_counter=prime_counter if isprime(number): prime_counter=prime_counter+1 else:prime_counter=prime_counter else:print number

OpenStudy (anonymous):

ignore the max count of 9001, just testing larger numbers. it does produce the right answer with correct max count.

OpenStudy (anonymous):

here is problem 2 of hw1 from math import * def isprime(startnumber): startnumber*=1.0 for divisor in range(2,int(startnumber*0.5)+1): if startnumber/divisor==int(startnumber/divisor): return False return True rawnumber=input('What number N to consider? ') prime_sum_counter=0 number=0 while(number<rawnumber): number=number+1.0 prime_sum_counter=prime_sum_counter if isprime(number): prime_sum_counter=prime_sum_counter+log(number) else:prime_sum_counter=prime_sum_counter else:print prime_sum_counter print rawnumber print prime_sum_counter/rawnumber

OpenStudy (anonymous):

post your code using dpaste.com

OpenStudy (anonymous):

problem 2: for this line: for divisor in range(2,int(startnumber*0.5)+1): int(startnumber*0.5) can be expressed as startnumber/2 does this line work? if startnumber/divisor==int(startnumber/divisor) it looks like both sides of the comparison are the same python does integer division - read this and look at the note in the table for the / operator http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex what is the purpose of the second line after your while statement prime_sum_counter=prime_sum_counter please use dpaste.com to post your code. It makes it easier for everyone. goto dpast.com - paste your code in the code block - select python for the syntax - click the paste it button - post the link here, thnx put some print statements in your code and print variable values, comparison/calculation results, accumulator values ... stuff like that. It will help you see what your code is doing.

OpenStudy (anonymous):

it works. the purpose of the prime_sum_counter= prime_sum_counter is to not alter the running sum if the number isn't prime.

OpenStudy (anonymous):

also the function compares decimal to int so if they are equal it is a valid divisor

OpenStudy (anonymous):

sorry - i missed that startnumber is a float

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!