I'am solving Euler's project and right now triyn to solve 9 task.  The solution I've found is in 3 nested loops:  for ai in range(1, 100):    for bi in range(1, 100):       for ci in range(1,100):           if ai + bi + ci == 25 and ai * ai = ci:              break   but when last if find the solution for-loops continues and I wanna break all of them.  Is it posible? I thougt about using some flags but it's additional check for each step and makes execution longer.
16336
Given the following code (that doesn't work):  while True:     #snip: print out current state     while True:         ok = get_input("Is this ok? (y/n)")         if ok.lower() == "y": break 2 #this doesn't work :(         if ok.lower() == "n": break     #do more processing with menus and stuff   Is there a way to make this work? Or do I have do one check to break out of the input loop, then another, more limited, check in the outside loop to break out all together if the user is satisfied?
38763
I was just getting my hands dirty solving some equations of the form $x=\phi(n)$ where $\phi(n)$ is Euler totient function. I know that $\phi(n)$ is even for $n\geq 3$. However, I am wondering that:     Can all even integers $x$ be expressed as $\phi(n)$ where $n$ is an integer?   I start doing some research, the first even number appears to hove no solution is $14$. According to , there is no solution to the equation $14=\phi(n)$ for $n\leq 500$. But does it have a solution for $n&gt;500$?  Moreover, what can you say about the above statement?
111272
Is there an easier way to break out of nested loops than throwing an exception? (In Perl, you can give labels to each loop and at least continue an outer loop.)    for x in range(10):     for y in range(10):         print x*y         if x*y &gt; 50:             "break both loops"   I.e., is there a nicer way than:  class BreakIt(Exception): pass  try:     for x in range(10):         for y in range(10):             print x*y             if x*y &gt; 50:                 raise BreakIt except BreakIt:     pass
95790
A friend was in need of an algorithm that would let him loop through the elements of an NxM matrix (N and M are odd). I came up with a solution, but I wanted to see if my fellow SO'ers could come up with a better solution.  I'm posting my solution as an answer to this question.  Example Output:  For a 3x3 matrix, the output should be:  (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1)    Furthermore, the algorithm should support non-square matrices, so for example for a 5x3 matrix, the output should be:  (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1) (2, -1) (2, 0) (2, 1) (-2, 1) (-2, 0) (-2, -1)
75314
Context  I am writing a computer program to do a brute-force search for a solution to a  which wanted to arrange four numbers with the four standard arithmetic operators and arrive at a given answer. While I have a solution to the original puzzle, I still want to write a general-case solving program for n numbers. I have broken down the problem into:   Arrangements of the numbers (n!) Arrangements of operators (4n-1) Arrangements of associativity. Or bracketing, if you will.   It is this last one where I cannot find the general-case answer to "how many will there be?" but if I am to write a program to solve it then I need my test cases to prove that my implementation is correct.  Progress  I have a notation for these, which operates by breaking the formula under consideration into Reverse Polish notation, and expressing it as 1 for a "Push number" operation, or 0 for "Pop two numbers, apply operator, push result" operation. It is clear that the four operators are binary operators, so the first two operations must necessarily be "Push" operations. Similarly, the last one must be a calculation.  Further, when counting N1 - the numbers of pushes - and N0 - the numbers of calculations - from the start up to any given point, N1 must always be greater than N0   By inspection, I have determined the following results:   n | C(n) |  ---+------+-  2 |   1  | 110  3 |   2  | 11010 11100  4 |   5  | 1101010 1101100 1110010 1110100 1111000  5 |  14  |    I think C6 is 42 but am not yet sure about that.  Question  What is the general-case value for Cn?
46321
The question asks to verify that each equation is true for every positive integer n.   The question is as follows:  $$1+ 3 + 5 + \cdots + (2n - 1) = n^2$$  I have solved the base step which is where $n = 1$.  However now once I proceed to the inductive step, I get a little lost on where to go next:  Assuming that k is true (k = n), solve for k+1:  (2k - 1) + (2(k+1) - 1) (2k - 1) + (2k+2 - 1) (2k - 1) + (2k + 1)   This is where I am stuck. Do I factor these further to obtain a polynomial of some sort? Or am I missing something?
51027
Given $m\in\mathbb{N}$, is there a general way to solve the equation \begin{equation} \varphi(n)=m \end{equation} where $\varphi$ is the Euler's totient function? For example, I want to solve for all the $n$'s such that $\varphi(n)=4$. Is there any way to solve it that are also applicable to many (if not general) cases?
2695
Some of you may recognize this as Project Euler's problem number 11. The one with the grid.  I'm trying to replicate the grid in a large multidimensional array, But it's giving me a syntax error and i'm not sure why  grid = [  [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 ], [ 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 ], [ 81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65 ], ...   And I get this error:     File "D:\development\Python\ProjectEuler\p11.py", line 3     [ 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91 , 08 ],        ^ SyntaxError: invalid token   Why is it throwing an error before the comma?
5520
I was asked to find the no. of ways to get from (0,0) to (8,8) with the 4 points of the shaded box unpassable, aka I can't use the 4 points namely (3,3)(4,4)(3,4)(4,3) while on my way to (8,8). Right and up movements.  I know it is something like a pascal triangle and worked out the solution of 4050 ways.  May I know if there are any faster means using permutation formula?  Thank you in advance.
75067
You're given a sequence of number: $1,\frac{1}{2},\frac{1}{3},\dots,\frac{1}{64}$.  A function takes two numbers in the sequence, say $a,b$ and replaces them both with $a+b+ab$. For example, we could take the initial sequence and apply the operation to the numbers $1$ and $1/64$ to obtain the new list $$\frac{1}{2}, \frac{1}{3},\cdots, \frac{1}{63}, \frac{33}{32}, \frac{33}{32}$$     Is there a solution so that after some number of steps, all terms in the sequence are equal? How many unique solutions are there?     Note that this question is different from the question asked , because the number of terms in that question changes, while in this question, the number of terms remains constant ($64$).  By the reasoning in the linked question, we can observe that the operation $a*b = a + b + ab = (a+1)(b+1)-1$ on $\mathbb{Q}$ is identical to the usual multiplication, under the isomorphism $(\mathbb{Q}, *) \to (\mathbb{Q}, \times)$ given by $x \mapsto x+1$.
112190
This question is a part of a TopCoder problem. I am learning algorithms, and just got stuck at this (not homework).  Suppose we have a set $A$ of integer elements, such that $n(A) = a$ (number of elements in $A$ is $a$). Now we have some $n$ blanks, each of which can be filled using the $a$ elements in A, repetition allowed. I want to find out the total number of such sequences of length $n$ which are in non decreasing order, and can be formed using the elements in $A$.  My thoughts reduce this problem to a dynamic programming approach, but I just wanted to find out if a straightforward formula is possible or not. Thanks.
74986
Let $\sigma \in S_n \;$ then $\; \sum_{i=1}^n|\sigma(i)-i|\;$ is an even number. I tried to show the statement for cyclic permutations first. However, I am not getting anywhere. The statement is trivial for transposition $(k,l)$. That´s all what I have found out so far. Can someone give me a hint why the statement is in fact true for any cyclic permutation ?
14717
The problem is: Find $C_n$, the number of all ternary strings (length $n≥0$) that don't have consecutive 1's and 2's, using combinatorics.   Any tips or advices?
26894
There are $200$ students in a class. Each student is a member of at least on of the clubs Eagles, Falcons, Tigers and Pythons.  If each club has $100$ students, then the number of students who are members of all the four clubs is at the most .....  If each club has $120$ students, then the number of students who are members of at the most two clubs  is at the least .....  If each club has $140$ students, then the number of students who are members of at least three clubs is at least ....   I came across this question and I tried to solve it using the $4$ circles Venn diagram but it is a very chaotic way to solve this (Too many variables come into picture and it all got messed up) and even I was only able to solve the first question. Any way or method to simplify the solution approach for this question? Any help is appreciated. Thanks!
57602
How do I find a recursion formula to solve the following question:  In how many sequences of length $n$, with elements from $\{{0,1,2,3,4}\}$, the difference between every 2 adjacent elements is $1$ or $-1$?  I've been trying to solve the problem for the last 5 hours maybe, I'm sure that I'm missing something..   This is a problem in combinatorics, and the solution must be by finding a recursion formula for the problem.   best solution I've come so far is that if we say that $a_n$ is the solution to the question, so let's count how many sequences of size $n-1$ there are that meet the requirement, and we complete the first element to make it a sequence of $n$ elements. so for $n-1$ there $a_{n-1}$ sequences, and for each sequence we have $2$ options to choose the first element (one option is to take a plus $1$ number from the number that start the $n-1$ sequence, and the other option to to take a minus $1$ number from the number that start the $n-1$ sequence), only problem as you figured out is that if the $n-1$ sequnce starts with $0$ or $4$ there is only $1$ option left for the first element in the sequnce.. so $a_n=2a_{n-1}$ won't work.  I think that the solution may involve using $2$ or maybe $3$ recursion formulas.  Hope I was clear enough.
19089
This is from one of the exercises in "", and is something I'm doing privately, not homework.  This is a variant on the classic towers of Hanoi, where all moves must be made clockwise only. The recursive solution is to consider $Q_n$ to be the number of moves to move a tower size $n$ one step across (e.g. from start $\rightarrow$ end post), and $R_n$ to be the number of moves to move it two (e.g start $\rightarrow$ end $\rightarrow$ spare).  The recursive solution is (sorry about the formatting, just getting to grips with tex):  $Q_n = 0 \quad \text{if n = 0}$  $Q_n = 2R_{n-1} + 1\quad \text{if n &gt; 0}$  $R_n = 0 \quad \text{if n = 0}$  $R_n = Q_n + Q_{n-1} + 1 \quad \text{if n &gt; 0}$  How can I prove these? I presume an inductive proof is required but everything I've tried to do just gets garbled into a mess of further decreasing $n$ values.  Explaining why these work is simple enough: $Q_n$ can be explained by the fact that you need to move a stack of size $n-1$ two steps forward followed by the $n$ piece, followed by moving the $n-1$ stack two steps again onto the $n$ piece=> $Q_n = R_{n-1} + 1 + R_{n-1} = 2R_{n-1}+1$.   The wordy proof for $R_n$ is similar: It consists of moving $R_{n-1}$ then 1 followed by $Q_n$, 1 and $R_{n-1}$ which will end us up with   $R_n = 2R_{n-1}+1+Q_{n-1}+1 = Q_n+Q_{n-1}+1$  But these are all just words that don't form any real proof... I've gone over the leading chapter and looked up some resources on proof by induction, but doing it with a recursion with two components has put me for a spin (having forgotten a lot of the math studied in the past certainly isn't helping either). If someone could run me through a rigorous proof for $n &gt; 0$ it would be much appreciated.  As an aside, any recommended learning resources on proof by induction would be appreciated as I'm still barely "getting it".
74748
was found from the Hot Network Questions on the right. A repost of a question in a way (from Mathematica SE), but I was wondering if the following puzzle could be done:  A quick translation:  Can anyone solve this? It's so hard! Fill in the numbers $1–9$ in the boxes, the numbers cannot repeat.  It has already been solved in the given link, the answers being (hidden for those who wish to try and solve it):   $17\times4=68+25=93$  The answer to the Mathematica question stated there's a way to solve it without brute-forcing, but I didn't understand it too well. How would one—if possible—solve this without using too complex softwares?