<br><br><div class="gmail_quote">On Tue, Jul 13, 2010 at 3:20 PM, Dipo Elegbede <span dir="ltr">&lt;<a href="mailto:delegbede@dudupay.com">delegbede@dudupay.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div>I was trying to write a code that prints prime numbers between 1 and 20.</div>
<div> </div>
<div>I have by myself seen that something is wrong with my code and also my brain.</div>
<div> </div>
<div>Could anyone be kind enough to tell me what to do....</div>
<div> </div>
<div>Where I am confused is how to test for other numbers without one and the number itself. It turns out that all numbers pass the condition I set so that would presuppose that all numbers are prime which is not.</div>
<div> </div>
<div>How exactly can I get it to run checks with other numbers such that it doesn&#39;t include the number itself and 1.</div>
<div> </div>
<div>The code is as follows:</div>
<div> </div>
<div>for i in range(1,20):<br>    <br>    if float(i) % 1 == 0 and float(i) % i == 0:<br>        print i, &#39;is a prime number&#39;<br></div></blockquote><div><br></div><div>Do you realize that all the numbers are divisible by 1. Hence float(i) % 1 _will be always zero_.</div>

<div>Now again, Do you realize that every number is divisible by itself. So float(i) % i will also always be zero.</div><div>Also are you using float when dealing with integers.</div><div>Steps:</div><div>* You should probably check whether a number is prime.</div>

<div>* Then you should add extra loop to check whether in a given range of numbers how many are prime. </div><div>* Now you optimize the code for lesse number of calculations, say for n check for divisibility only until sqrt(n)</div>

<div>* Now you read wiki, read about sieve and apply sieve of erastothenes, to make the code fast.</div><div>* Again memoize your code, so that it becomes more fast.</div><div>With all this done, you have a pretty handy code with yourself, coded by you.</div>

<div>* Now solve this problem - <a href="https://www.spoj.pl/problems/PRIME1/">https://www.spoj.pl/problems/PRIME1/</a> , which can&#39;t be solved unless your code is highly optimized.</div><div>* If you have more curiosity, look for sieve of atkin.</div>

<div><br></div><div>Finish these and you are well off with primes :)</div><div><br></div><div>~l0nwlf</div><div><br></div><div><br></div></div><br>