x = 1 / 2 + 3 // 3 + 4 ** 2 # Why is this equivalent to 17?
y = 1 / 2 + 3 # This is equivalent to 3.5
z = 1 / 2 + 3 // 3 # This should be equivalent to 3.5 // 3
t = 3.5 // 3 + 4 ** 2 # Why is this 17 but the original statement is 17.5?
Why are the expressions for t and x providing different results? Are they not equivalent?
(Original image)
It's 17.5 because your statement is as follows:
1/2 - this is evaluated to 0.5
3 // 3 - this is evaluated to 1
4 ** 2 - this is evaluated to 16
16 + 1 + 0.5 = 17.5
You need to understand the order of operations in your initial statement:
1 / 2 + 3 // 3 + 4 ** 2
This can be bracketed according to standard order of operations (BODMAS or some variant):
(1 / 2) + (3 // 3) + (4 ** 2)
Which then evaluates as above. Your confusion stems from the fact that 1 / 2 + 3 // 3 is not equivalent to (1/2 + 3) // 3, but instead equivalent to (1/2) + (3 // 3) - they're both division, so they'll both take precedence over the addition operator.
def sum(a):
if a==1:
s=1
else:
s=1+2*sum(a-1)
return s
function:calculate the sum of the sequence of number. Its common ratio is 2, last term is 2^( a-1) and first term is 1.
Why does it use s=1+2*sum(a-1) to implement the function?
def sum1(a):
if a==1:
s=1
else:
s=1+2*sum1(a-1)
return s
What this function does, let's take a=4.
(1) s = 1 + 2*sum1(4-1) = 1 + 2*sum1(3) = 1 + 2*s2
(2) s2 = 1 + 2*sum1(3-1) = 1 + 2*sum1(2) = 1 + 2*s3
(3) s3 = 1 + 2*sum(2-1) = 1 + 2*sum(1) = 1 + 2*s4 = 1+2 = 3
Going BackWard : (3 * 2 + 1 ) * 2 + 1 = (7) * 2 + 1 = 15
Do it for bigger numbers, you will notice that this is the formula of 2^a - 1.
Print 2^a and 2^a - 1
Difference: (4, 3)
Difference: (8, 7)
Difference: (16, 15)
Difference: (32, 31)
Difference: (64, 63)
Difference: (128, 127)
Difference: (256, 255)
#Zhongyi I'm understanding this question as asking "how does recursion work."
Recursion exists in Python, but I find it a little difficult to explain how it works in Python. Instead I'll show how this works in Racket (a Lisp dialect).
First, I'lll rewrite yoursum provided above into mysum:
def yoursum(a):
if a==1:
s=1
else:
s=1+2*yoursum(a-1)
return s
def mysum(a):
if a == 1:
return 1
return 1 + (2 * mysum(a - 1))
for i in range(1, 11):
print(mysum(i), yoursum(i))
They are functionally the same:
# Output
1 1
3 3
7 7
15 15
31 31
63 63
127 127
255 255
511 511
1023 1023
In Racket, mysum looks like this:
#lang racket
(define (mysum a)
(cond
((eqv? a 1) 1)
(else (+ 1 (* 2 (mysum (sub1 a)))))))
But we can use language features called quasiquoting and unquoting to show what the recursion is doing:
#lang racket
(define (mysum a)
(cond
((eqv? a 1) 1)
(else `(+ 1 (* 2 ,(mysum (sub1 a))))))) ; Notice the ` and ,
Here is what this does for several outputs:
> (mysum 1)
1
> (mysum 2)
'(+ 1 (* 2 1))
> (mysum 3)
'(+ 1 (* 2 (+ 1 (* 2 1))))
> (mysum 4)
'(+ 1 (* 2 (+ 1 (* 2 (+ 1 (* 2 1))))))
> (mysum 5)
'(+ 1 (* 2 (+ 1 (* 2 (+ 1 (* 2 (+ 1 (* 2 1))))))))
You can view the recursive step as substituting the expression (1 + 2 * rest-of-the-computation)
Please comment or ask for clarification if there are parts that still do not make sense.
Formula Explanation
1+2*(sumOf(n-1))
This is not a generic formula for Geometric Progression.
this formula is only valid for the case when ratio is 2 and first term is 1
So how it is working.
The Geometric Progression with first term = 1 and r = 2 will be
1,2,4,6,8,16,32,64
FACT 1
Here you can clearly see nth term is always equals to sumOf(n-1) terms + 1
Let declare an equation from fact 1
n = sumOf(n-1) + 1 =======> Eq1.
Let put our equation to test
put n = 2 in Eq1
2 = sumOf(2-1) + 1
we know that sumOf(1) is 1 then
2 = 2 ==> proved
so if n = sumOf(n-1)+1 then
FACT 2
Sum of n term is n term + sum(n-1) terms
Lets declare an Equation from FACT 2
sumOf(n) = sumOf(n-1) + n ==> eq2
let us put eq1 in eq2 i.e. n = sumOf(n-1) + 1
sumOf(n) = sumOf(n-1) + sumOf(n-1) + 1 ==> eq3
Simplifying
sumOf(n) = 2 * sumOf(n-1) + 1
Rearranging
sumOf(n) = 1 + 2 * sumOf(n-1) ==> final Equation
Now Code this equation
we know sumOf 1st term is alway 1 so, this is our base case.
def sumOf(a):
if a==1:
return 1
so now sum of first n terms will be 1 + 2 * sumOf(n-1) ==> From final Equation
put this equation in else part
def sumOf(a):
if a==1:
return 1
else:
return 1 + 2 * sumOf(a-1)
Teaching myself coding, what is the order of operations for this line of code?
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
I attempted to do remainder and division first so I got 3 + 2 + 1 - 5 + 0 - 0.25 / 4 + 6. Then I completed AS from left to right and got 0.075. Totally wrong because LPTHW puts it at 7. Please offer detailed operation order.
I Googled Python order of operation, but results are not too instructively detailed.
print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
Expected result is 7, but obtained 0.075
It depends on which Python version you are using.
In Python 2, the / operator defaults to integer division, so 1 / 4 == 0.
On the other hand, in Python 3, the / operator defaults to true division, so 1 / 4 == 0.25. You must use // to achieve integer division in Python 3.
Regardless, Python still follows the classical PEMDAS order of operations, so the modulus and division still happen first, followed by addition and subtraction from left to right.
Here's how the problem reduces after you do the modulus and division in both versions:
Python 2
(3 + 2 + 1 - 5 + 0 - 0 + 6) == 7
Python 3
(3 + 2 + 1 - 5 + 0 - 0.25 + 6) == 6.75
*, /, //, % have higher precedence than + and -. So you should first calculate 4 % 2 = 0 and 1 / 4 = 0 (in Python 2.7), and then do the rest of the calculation from left to right.
In Python 2, / uses integer division if both its arguments are integers. That means that 1/4 == 0, since integer division will round down. Then:
= 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
= 3 + 2 + 1 - 5 + 0 - 0 + 6
= 7
To get 6.75 in Python 2 (the expected answer when done on paper), make one of the operands a float:
>> 3 + 2 + 1 - 5 + 4 % 2 - 1.0 / 4 + 6
# ^
>> 6.75
This isnt necessary in Python 3 because / defaults to returning a float.
When I was little, I played the next game with my cousins:
"Zero in the car's license plates": by using the numbers in the car's license plate numbers and using the elementary operations (sum, subtraction, product and division) once each, you had to find an order that equaled 0.
For example, if we have the following license plate:
2591 --> (2*5)-(9+1) = 0
2491 --> (2*4)+1 -9 = 0
I would like to make a program in Haskell or Python that it is able to play this game, and print the steps that gives a result of 0.
getZero 2491 = 2*4+1-9
getZero 2591 = 2*5-9+1
Maybe its impossible to make this; I hope that you can help me.
This is probably off topic but a fun toy puzzle.
An easy way to think of the issue is in perhaps four separate parts:
Boring plumbing for the arguments and printing the results
Construct all possible expressions of binary operations of the given numbers.
Interpret these expressions to obtain a numeric literal and drop any result that is not zero.
Pretty print the expression.
We could perform extra steps, such as filtering out duplicates based on algebraic rules (a + b and b + a are morally the same), but I skipped that.
The boring plumbing is just getting our plate numbers (one for each argument), breaking those down into digits, running our computation, and printing the solution.
import Data.Foldable
import Data.List
import System.Environment
main :: IO ()
main =
do ns <- getArgs
for_ ns $ \n -> do
let digitList = map (read . (:[])) (n :: String) :: [Int]
putStrLn $ "---------- " ++ show n ++ " ----------"
putStrLn $ unlines (map render (solutions digitList))
Construction The real fun is in this construction of a binary tree of operations and leafs with the literals. First we define our expression language:
data Expr = Add Expr Expr
| Sub Expr Expr
| Mul Expr Expr
| Div Expr Expr
| Lit Int
deriving (Eq, Ord, Show)
And we can use these expressions along with a Haskell list monad to build all possible expressions (assuming a non-empty list as input):
operations :: [Expr -> Expr -> Expr]
operations = [Add, Sub, Mul, Div]
exprsOf :: [Expr] -> [Expr]
exprsOf [term] = [term]
exprsOf xs =
do x <- xs
y <- (delete x xs)
o <- operations
exprsOf (o x y : delete y (delete x xs))
That is, x is one of the elements in the original set of expressions. y is another element (but not x). o is one of our legal operations (addition, subtraction, etc). And we recursively reduce this list size till we are left with the top level expression (variable name term). If you don't understand the operation that's OK - specific parts that confuse you would make a fine (on topic) question.
Interpretation With the expressions built we can now interpret them and filter out any that don't result in zero.
The interpreter is just using addition (+) when we see our Add constructor and same for the other operations. I lifted everything into the Maybe Applicative because I didn't want division by zero or with a remainder to show up in our results.
interp :: Expr -> Maybe Int
interp (Lit n) = Just n
interp (Add a b) = (+) <$> interp a <*> interp b
interp (Sub a b) = (-) <$> interp a <*> interp b
interp (Mul a b) = (*) <$> interp a <*> interp b
interp (Div a b) | interp b == Just 0 = Nothing
| interp b == Nothing = Nothing
| otherwise =
case divMod <$> interp a <*> interp b of
Nothing -> Nothing
Just (x,0) -> Just x
_ -> Nothing -- Ignore uneven division
Applying this interpretation is just a matter of filtering for Just 0:
solutions :: [Int] -> [Expr]
solutions xs = filter ((== Just 0) . interp) $ exprsOf (map Lit xs)
Rendering Finally, there's a pretty ugly render function just to emit the proper parenthesis so we see the right order of operations:
render :: Expr -> String
render (Lit n) = show n
render (Add a b) = "(" ++ render a ++ " + " ++ render b ++ ")"
render (Sub a b) = "(" ++ render a ++ " - " ++ render b ++ ")"
render (Mul a b) = "(" ++ render a ++ " * " ++ render b ++ ")"
render (Div a b) = "(" ++ render a ++ " / " ++ render b ++ ")"
Example Run
*Main> :main 2591
---------- "2591" ----------
(((2 * 5) - 9) - 1)
(1 - ((2 * 5) - 9))
(((2 * 5) - 1) - 9)
(9 - ((2 * 5) - 1))
((9 - (2 * 5)) + 1)
(1 + (9 - (2 * 5)))
((9 + 1) - (2 * 5))
((2 * 5) - (9 + 1))
((1 - (2 * 5)) + 9)
(9 + (1 - (2 * 5)))
((1 + 9) - (2 * 5))
((2 * 5) - (1 + 9))
(((5 * 2) - 9) - 1)
(1 - ((5 * 2) - 9))
(((5 * 2) - 1) - 9)
(9 - ((5 * 2) - 1))
((9 - (5 * 2)) + 1)
(1 + (9 - (5 * 2)))
((9 + 1) - (5 * 2))
((5 * 2) - (9 + 1))
((1 - (5 * 2)) + 9)
(9 + (1 - (5 * 2)))
((1 + 9) - (5 * 2))
((5 * 2) - (1 + 9))
(((9 + 1) / 2) - 5)
(5 - ((9 + 1) / 2))
(((9 + 1) / 5) - 2)
(2 - ((9 + 1) / 5))
((2 * 5) - (9 + 1))
((9 + 1) - (2 * 5))
((5 * 2) - (9 + 1))
((9 + 1) - (5 * 2))
(((1 + 9) / 2) - 5)
(5 - ((1 + 9) / 2))
(((1 + 9) / 5) - 2)
(2 - ((1 + 9) / 5))
((2 * 5) - (1 + 9))
((1 + 9) - (2 * 5))
((5 * 2) - (1 + 9))
((1 + 9) - (5 * 2))
This isn't the best implementation, because there are quite a lot of repeats, but does get a result.
Using itertools, I generated every different state (permutation) that the numbers and operations could be in, e.g.:
[('1', '2', '3', '4'), ('1', '2', '4', '3'), ('1', '3', '2', '4'), ('1', '3', '4', '2'), ('1', '4', '2', '3'), ....... ('4', '1', '3', '2'), ('4', '2', '1', '3'), ('4', '2', '3', '1'), ('4', '3', '1', '2'), ('4', '3', '2', '1')]
[('+', '-', '*'), ('+', '*', '-') ...... ('/', '-', '*'), ('/', '*', '-')]
...then, I made a function that adds brackets to every possible state of the calculation:
[1,'+',2,'-',3,'*',4] becomes:
1 + 2 - 3 * 4
( 1 + 2 ) - ( 3 * 4 )
( 1 + 2 - 3 ) * 4
1 + ( 2 - 3 * 4 )
( 1 + 2 ) - 3 * 4
1 + 2 - ( 3 * 4 )
1 + ( 2 - 3 ) * 4
... and then, evaluated each different possibility, and if it was zero I printed it out.
Heres all the code:
from itertools import permutations, combinations, chain
import sys
operations = ['+', '-', '*', '/'] # basic operations
# add every combination of brackets to the calculation
def addBrackets(calc):
possibilities = []
possibilities.append(" ".join(calc))
possibilities.append(" ".join(str(s) for s in list(chain(["("], calc[:3], [")"], [calc[3]], ["("], calc[4:], [")"]))))
possibilities.append(" ".join(str(s) for s in list(chain(["("], calc[:-2], [")"], calc[-2:]))))
possibilities.append(" ".join(str(s) for s in list(chain(calc[:2], ["("], calc[2:], [")"]))))
possibilities.append(" ".join(str(s) for s in list(chain(["("], calc[:3], [")"], calc[3:]))))
possibilities.append(" ".join(str(s) for s in list(chain(calc[:4], ["("], calc[4:], [")"]))))
return possibilities
def getZero(n): # 2491
nums = [x for x in str(n)] # [2, 4, 9, 1]
while len(nums) < 4:
nums = ['0'] + nums # add zeroes if the input was less than 1000 e.g. [5, 3, 1] -> [0, 5, 3, 1]
possible_number_order = list(permutations(nums)) # get every number order
possible_op_order = list(combinations(operations, 3)) # get combinations of 3 of the operations
possible_op_permutations = list(chain(list(permutations(p)) for p in possible_op_order)) # chain together all the permutations of each combination
possible_op_order = []
for perms in possible_op_permutations:
possible_op_order.extend(perms) # add all the lists into one
for n in possible_number_order: # for each number order
for op in possible_op_order: # for each operation order
calculation = [n[0],op[0],n[1],op[1],n[2],op[2],n[3]] # create a list with the order of the operation
for calc in addBrackets(calculation): # for each bracket position
try:
result = eval(calc) # evaluate
if abs(result) <= 0.001: # to check for float comparison
print("{} = {}".format(calc, 0))
except: # ZeroDivisionError
continue
if __name__ == "__main__":
for user_input in [int(n) for n in sys.argv[1:]]:
print("-------{0:04}-------".format(user_input)) # print 0 padded output
getZero(user_input)
Enter the input as command line arguments:
$ python3.6 zerogame.py 2491 2591
-------2491-------
( 2 * 4 - 9 ) + 1 = 0
( 2 * 4 ) - 9 + 1 = 0
( 2 * 4 ) + ( 1 - 9 ) = 0
( 2 * 4 + 1 ) - 9 = 0
( 2 * 4 ) + 1 - 9 = 0
2 * 4 + ( 1 - 9 ) = 0
2 + ( 1 - 9 ) / 4 = 0
( 4 * 2 - 9 ) + 1 = 0
( 4 * 2 ) - 9 + 1 = 0
( 4 * 2 ) + ( 1 - 9 ) = 0
( 4 * 2 + 1 ) - 9 = 0
( 4 * 2 ) + 1 - 9 = 0
4 * 2 + ( 1 - 9 ) = 0
4 + ( 1 - 9 ) / 2 = 0
9 - ( 2 * 4 + 1 ) = 0
9 - ( 4 * 2 + 1 ) = 0
9 - ( 1 + 2 * 4 ) = 0
9 - ( 1 + 4 * 2 ) = 0
( 1 + 2 * 4 ) - 9 = 0
1 + ( 2 * 4 - 9 ) = 0
1 + ( 2 * 4 ) - 9 = 0
( 1 + 4 * 2 ) - 9 = 0
1 + ( 4 * 2 - 9 ) = 0
1 + ( 4 * 2 ) - 9 = 0
( 1 - 9 ) +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