I understand how generators and iterators work, but I don't see why generators are supposed to be so useful. I saw multiple tutorials that use a prime number finder as an example, however, I always also saw ways how to achieve the same without using a generator (without storing the numbers), for example I usually found things like this:  def gen_prim(num):     while True:         if is_prime(num): # just some function that returns true or false if num is a prime             yield num         num += 1  for i in gen_prim(1000):     if i &lt; 1000:         print(i)     else:         break   However, instead of creating a generator in the first place, we could just instead of yielding num, do print(num) inside of gen_prim right away, as we're going to do that later anyways, and can thus remove the for loop.   So, as said, I don't understand how generators are beneficial, as to me it seems they just produce more code. I would greatly appreciate an explanation of why they are considered useful and how exactly I can use them that they are beneficial to me, because as you saw, they don't seem to help me very much.
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.
372
What is the difference between the search() and match() functions in the ?  I've read the  (), but I never seem to remember it.  I keep having to look it up and re-learn it.  I'm hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head.  Or at least I'll have a better place to return with my question and it will take less time to re-learn it.
42066
My question is: Can you find a cyclic group with n generators?  I know that zero (or any other identity element for that matter) is included, so there would be for $Z_n$ at most n-1 generators. However, is it possible to say that $Z_{n+1}$ could provide n generators?  Any help would be much appreciated! If you wouldn't mind, I would appreciate it if you provide enough detail to really help me understand so I can learn. Thanks in advance!
94366
Well, just another idea came up into my mind and i have no idea how to solve it :D Is there infinitely many prime numbers, which are not repunits and their inverse is also prime? (For example, inverse of 31 is 13 which is also prime. i didn't have any other name to describe the function!) P.S:I now know they are called s.
52574
For no particular reason I stumbled upon the sequence/set of all numbers where all substrings of the decimal representation is prime (A085823). It's quite easy to see that this must be a finite set. My approach was that the digits must alter between 3 and 7 eventually (since a prime can't end with 2 or 5 for larger than that and a prime can't consist of just two non-1 digits, those would be dividable by 2, 5 or 11) and we can see where it ends.  But if we change the base that approach fails. For example in base 8 a prime can end in 3, 5 or 7 AFAICS so such numbers can end in a altering sequence of 3, 5 and 7s but having tree digits to alter between there's always a way to vary the sequence.  I tried to write a python snippet to produce such numbers in different bases and for those I've tested the program seem to hang (ie don't seem to find any more numbers). Is it true that there are only a finite number of such numbers for every base?
34320
I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems.  So, sometime in the very far past I went to try and find if there existed any "pattern" or text-book way of transforming a common recursion approach to iteration and found nothing. Or at least nothing that I can remember it would help.   Are there general rules? Is there a "pattern"?
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?
65805
How can I build a numpy array out of a generator object?  Let me illustrate the problem:  &gt;&gt;&gt; import numpy &gt;&gt;&gt; def gimme(): ...   for x in xrange(10): ...     yield x ... &gt;&gt;&gt; gimme() &lt;generator object at 0x28a1758&gt; &gt;&gt;&gt; list(gimme()) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] &gt;&gt;&gt; numpy.array(xrange(10)) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) &gt;&gt;&gt; numpy.array(gimme()) array(&lt;generator object at 0x28a1758&gt;, dtype=object) &gt;&gt;&gt; numpy.array(list(gimme())) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])   In this instance, gimme() is the generator whose output I'd like to turn into an array. However, the array constructor does not iterate over the generator, it simply stores the generator itself. The behaviour I desire is that from numpy.array(list(gimme())), but I don't want to pay the memory overhead of having the intermediate list and the final array in memory at the same time. Is there a more space-efficient way?
29728
In other words, can I do something like  for() {     for {        for {        }     } }   Except N times? In other words, when the method creating the loops is called, it is given some parameter N, and the method would then create N of these loops nested one in another?  Of course, the idea is that there should be an "easy" or "the usual" way of doing it. I already have an idea for a very complicated one.
25235
What is the purpose of  in Python — especially with respect to when I would want to use it, and when not?
66287
On a Google search, I've notice that often the "trick" involves factoring the expression (and thus showing that the expression isn't prime), but I can't see it. Is this how you would go about it in this case, or would that be a dead end? Any hints?
12628
Is there a benefit to using one over the other? In Python 2, they both seem to return the same results:  &gt;&gt;&gt; 6/3 2 &gt;&gt;&gt; 6//3 2
12059
I understand the use of pseudo-random number generators. I am not getting mixed up between these and "real" random number generators.  However, I don't understand for what a real random number generator is used. If it is not deterministic, how can it be used in an algorithm?
70314
How to find generators of $(\mathbb Z_{17}-\{0\},\times)$?  Is there a faster way to find generators than trying every element in the group?  I know that for additive group, if a number say m is relatively prime to n (in this case 17), then it's a generator of that group. Do we have something like this for multiplicative group?
71362
I can see that this works for any integer $n$, but I can't figure out why this works, or why the number $42$ has this property.
42504
I'm going through the problems on projecteuler.net to learn how to program in Erlang, and I am having the hardest time creating a prime generator that can create all of the primes below 2 million, in less than a minute. Using the sequential style, I have already written three types of generators, including the Sieve of Eratosthenes, and none of them perform well enough.  I figured a concurrent Sieve would work great, but I'm getting bad_arity messages, and I'm not sure why. Any suggestions on why I have the problem, or how to code it properly?   Here's my code, the commented out sections are where I tried to make things concurrent:   -module(primeserver). -compile(export_all).  start() ->     register(primes, spawn(fun() -> loop() end)).  is_prime(N) -> rpc({is_prime,N}).  rpc(Request) ->     primes ! {self(), Request},     receive         {primes, Response} ->             Response     end.  loop() ->     receive         {From, {is_prime, N}} ->             if                 N  From ! {primes, false};                 N =:= 2 -> From ! {primes, true};                 N rem 2 =:= 0 -> From ! {primes, false};                 true ->                     Values = is_not_prime(N),                     Val = not(lists:member(true, Values)),                     From ! {primes, Val}             end,             loop()     end.  for(N,N,_,F) -> [F(N)]; for(I,N,S,F) when I + S  [F(I)|for(I+S, N, S, F)]; for(I,N,S,F) when I + S =:= N -> [F(I)|for(I+S, N, S, F)]; for(I,N,S,F) when I + S > N -> [F(I)].  get_list(I, Limit) ->     if         I              [I*A || A              []     end.  is_not_prime(N) ->     for(3, N, 2,          fun(I) ->              List = get_list(I,trunc(N/I)),             lists:member(N,lists:flatten(List))         end         ).      %%L = for(1,N, fun() -> spawn(fun(I) -> wait(I,N) end) end),     %%SeedList = [A || A       %%      lists:foreach(fun(X) ->     %%              Pid ! {in_list, X}      %%                end, SeedList)     %%        end, L).  %%wait(I,N) -> %%  List = [I*A || A  lists:member(X,List) %%  end.
71065
I see a lot of Q/A where persons are trying to generate a specific key pair using static data such as a password. Now say we use a known PRNG (dangerous assumption), seeded with a the data as a static seed, would this always generate the same key pair?  I presume it would generate the same key pair if you would use the exact same generation method - the application would be deterministic. I would however expect differences between implementations. The random data could be used differently, the sizes of p and q could differ and there may be subtle differences when choosing the prime numbers themselves.