I wrote this simple python function to calc:  def isPrime(number):     if number == 2:         return True      elif number &gt; 1 and number % 2 != 0:         for current in range(3, number):             if number % current == 0:                 return False          return True   And I'm calling it to print out the sum of all prime numbers from 1 to 2 million, as in project   However it's terribly slow, and I was wondering if I could tackle the same problem using generators? But I don't really understand generators in python completely..  Any help on how to solve this problem more efficiently would be appreciated! Thanks :)
12929
Could someone please tell me what I'm doing wrong with this code? It is just printing 'count' anyway.  I just want a very simple prime generator (nothing fancy).   import math  def main():     count = 3     one = 1     while one == 1:         for x in range(2, int(math.sqrt(count) + 1)):             if count % x == 0:                  continue             if count % x != 0:                 print count          count += 1
113972
Are there any simple methods for calculating the digits of $\pi$? Computers are able to calculate billions of digits, so there must be an algorithm for computing them. Is there a simple algorithm that can be computed by hand in order to compute the first few digits?
114729
Like for $\pi$, we have an algorithm/infinite series that can give us the first 50 decimal places in about 3 terms. So if I wasn't to calculate like $\ln(25551879\cdots)$ (a really huge integer, most likely a prime), upto 100 decimal places, what will be the algorithm I should use or is used worldwide and how efficient is it? I know that the Taylor series is rather slow in its work, so any other algorithm in which this is computed?
70466
Given $x\in\mathbb R_+$ and $m,n\in\mathbb Z_+$, is it true that $$\bigg\lfloor\frac{\lfloor \frac{x}{m}\rfloor}{n}\bigg\rfloor=\bigg\lfloor \frac{x}{mn}\bigg\rfloor?$$    Thanks for at least three different and convincing proofs! I'll use this result  trying to code the prime counting formula from Wikipedia $(2)$:  Given $m$, select $y$ so that  $\sqrt[3]{m}\le y\le\sqrt{m}$ and let $n=\pi(y)$. Then  $(1)\;$ $\pi(m)=\phi(m,n)+n-1-P_2(m,n)$, where  $(2)\;$ $\displaystyle \phi(m,n)=\phi(m,n-1)-\phi\Big(\frac{m}{p_n},n-1\Big)$,  $\;\phi(m,0)=\lfloor{m}\rfloor$ and  $(3)\;$ $\displaystyle P_2(m,n)=\sum_{y&lt;p\le\sqrt{m}} \Big(\pi\Big(\frac{m}{p}\Big)-\pi(p)+1\Big)$, $\;p$ is a prime.
23841
Building on , I was thinking about sensible, intuitive ways to do that Programming 101 chestnut of displaying integers in base-2 form.  This is the best I came up with, but I'd like to replace it with a better algorithm, or at least one that should have screaming-fast performance.    def num_bin(N, places=8):     def bit_at_p(N, p):         ''' find the bit at place p for number n '''         two_p = 1 &lt;&lt; p   # 2 ^ p, using bitshift, will have exactly one                          # bit set, at place p         x = N &amp; two_p    # binary composition, will be one where *both* numbers                          # have a 1 at that bit.  this can only happen                           # at position p.  will yield  two_p if  N has a 1 at                           # bit p         return int(x &gt; 0)      bits =  ( bit_at_p(N,x) for x in xrange(places))     return "".join( (str(x) for x in bits) )      # or, more consisely      # return "".join([str(int((N &amp; 1 &lt;&lt; x)&gt;0)) for x in xrange(places)])
14484
I have found from a site this formula:    Ok.I have found that this formula is correct.see the reason below.  This part of formula is always $1$ or $zero$.    it's zero when $(2m)!+1$ isn't dividable to $2m+1$. And when it's zero this formula will give us the number 2.And it's 1 when (2m+1)! is dividable to 2m+1.Using Wilson's theorem if $(2m)!+1$ is dividable to $2m+1$ then $2m+1 $is prime.From these we can know that this formula always gives primes.But here is a big problem.This formula is very hard to use and it gives us many times the number $2$.My question is this is there a better formula for finding primes?  I have tried a lot of of formula.The best I have seen after this is mersenne's formula I don't liked this because it don't gives always a prime out.sometimes it generates not a prime number.I really believe that there is any formula that generates prime ordered because they are so much random but is there a formula that always gives out primes an it's easy to use?
22154
I was reading about primality test and at the  page it said that we just have to test the divisors of $n$ from $2$ to $\sqrt n$, but look at this number:  $$7551935939 = 35099 \cdot 215161$$  Since $\sqrt {7551935939} \approx 86901,875$ so basically I would only have to check the divisors from $2$ to $90000$, but one of the divisors ($215161$) is greater than $90000$.  Also, do you guys have some ideas to improme my primality test?
72165
$$\sum_{n = 1}^\infty \frac{(-1)^n}{p_n}$$ where $p_n$ is the $n$th prime.  I have computed this to 10000 rather than infinity. My results suggest that convergence does happen but it's very slow. But I can't even be sure about the first few digits: $-0.26959$?  I have looked at the "questions that may already have your answer" and some of the "similar questions," but they involve somewhat different formulas.
56984
I am a software engineer but try to keep up with maths as I really enjoyed the subject at school. I just saw a great TED talk:   The talk states that the current largest known prime is $2^{57885161}-1$  This got me thinking - how does one actually prove whether a number this large is prime or not?  Is it just done by brute force via a computer algorithm or are there any well known mathematical techniques for doing so?  I would be really grateful for any pointers as I am eager to learn about these techniques (if they exist).
66553
Is that true that all the prime numbers are of the form $6m \pm 1$ ?  If so, can you please provide an example?  Thanks in advance.
60173
I'm starting to learn Python and I've come across generator functions, those that have a yield statement in them.  I want to know what types of problems that these functions are really good at solving.
4177
Is there a way to step between 0 and 1 by 0.1?   I thought I could do it like the following, but it failed:  for i in range(0, 1, 0.1):     print i   Instead, it says that the step argument cannot be zero, which I did not expect.
31694
Given a function which produces a random integer in the range 1 to 5, write a function which produces a random integer in the range 1 to 7.   What is a simple solution? What is an effective solution to reduce memory usage or run on a slower CPU?
46866
Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it is faster) or what besides that is different about  for i in range(0, 20): for i in xrange(0, 20):
17009
I am looking for a command that will accept (as input) multiple lines of text, each line containing a single integer, and output the sum of these integers.  As a bit of background, I have a log file which includes timing measurements. Through grepping for the relevant lines and a bit of sed reformatting I can list all of the timings in that file.  I would like to work out the total. I can pipe this intermediate output to any command in order to do the final sum.  I have always used expr in the past, but unless it runs in RPN mode I do not think it is going to cope with this (and even then it would be tricky).  How can I get the summation of integers?
2785
I have a list of integers that I would like to convert to one number like:  numList = [1, 2, 3] num = magic(numList)  print num, type(num) &gt;&gt;&gt; 123, &lt;type 'int'&gt;   What is the best way to implement the magic function?  EDIT  I did find , but it seems like there has to be a better way.
79314
Suppose that there is a prime number. Now I want to approximate the next prime number. (It does not have to be exact.) What would be the time-efficient way to do this?  Edit: what happens if we limit the case to the prime number of the form $4k+1$ where k is a natural number?   Edit: it's fine to replace approximate prime number with finding any prime number that is bigger than the given prime number in a time-efficient way.