Floor division and modulo with negative number [duplicate] - python-3.x

This question already has answers here:
Floor division with negative number
(4 answers)
Closed 2 years ago.
print(-2//4)
The output is -1. I do not understand the logic. Why the negative modulo?

(-2) ÷ 4 gives -1 remainder 2.
The relation that has to be correct is that for integers a and b
a = (a//b) * b + (a%b)
In this case, b is 4, and the remainder a%b must be between 0 (inclusive) and 4 (exclusive). So the only values for a//b and a%b that work are
a//b = -1
a%b = 2
which gives
-2 = -1 * 4 + 2
a a//b b a%b
TLDR
The precise value of (-2) / 4 is -0.5. The greatest integer not greater than -0.5 is -1.

Related

recursive function does not work as expected

Could someone explain the code? I just can not understand why this code gives output like this:
1
3
6
10
15
21
I expected the code to give something like this:
1
3
5
7
9
11
What am I missing here?
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k-1)
print(result)
else:
result = 0
return result
tri_recursion(6)
For your recursive function, the termination condition is k=0.
It's clear that if k=0, tri_recursion(0) = 0.
If k=1, tri_recursion(1) = 1 + tri_recursion(0), which from above, is 1 + 0 or 1.
If k=2, tri_recursion(2) = 2 + tri_recursion(1), which from above, is 2 + 1 or 3.
If k=3, tri_recursion(3) = 3 + tri_recursion(2), which from above, is 3 + 3 or 6.
If k=4, tri_recursion(4) = 5 + tri_recursion(3), which from above, is 4 + 6 or 10.
If k=5, tri_recursion(5) = 4 + tri_recursion(4), which from above, is 5 + 10 or 15.
If k=6, tri_recursion(6) = 6 + tri_recursion(5), which from above, is 6 + 15 or 21.
See the pattern?
Your code is calculating the sum of numbers up to n where n is 6 in the above case. The print statement prints the intermediate results. Hence the output 1 3 6 10 15 21.
1 - The sum of numbers from 0 to 1
3 - The sum of numbers from 0 to 2
6 - The sum of numbers from 0 to 3
10 - The sum of numbers from 0 to 4
15 - The sum of numbers from 0 to 5
21 - The sum of numbers from 0 to 6

Why does Excel average gives different result?

Here's the table:
Should not they have the same result mathematically? (the average score of the per column and per row average)
The missing cells mean that your cells aren't all weighted evenly.
For example, row 11 has only two cells 82.67 and 90. So for your row average for row 11 they are weighted much more heavily than in your column averages where they are 1/13 and 1/14 of a column instead of 1/2 of a row.
Try filling up all the empty cells with 0 and the averages should match.
Taking a more extreme version of Ruslan Karaev's example:
5 5 5 | 5
1 | 1 Average of Average of Rows = (5 + 1 + 0) / 3 = 2
0 | 0
-----
2 5 5
Average of Average of Columns = (2 + 5 + 5) / 3 = 4
Yes, for example, the following two expressions:
/ a + b X + Y \ / a + X b + Y \
( ----- + ----- ) ( ----- + ----- )
\ 2 2 / \ 2 2 /
------------------- -------------------
2 2
are indeed mathematically equivalent, both coming out to be (a + b + X + Y) / 4.
However, short of having enough sufficient precision to store values, you may find that rounding errors accumulate differently depending on the order of operations.
You can see this sort of effect in a much simpler example if you assume a 3-digit precision and divide one by three, then multiply the result of that by three again:
1 / 3 -> 0.333, 0.333 x 3 -> 0.999
Contrast that with doing the operations in the oppisite order:
1 x 3 = 3, 3 / 1 = 1

Python Modulo Function

I understand that the Modulo function returns the remainder of a division problem.
Ex: 16 % 5 = 3 with a remainder of 1. So 1 would be returned.
>>> 1 % 3 Three goes into 1 zero times remainder 1
1
>>> 2 % 3 Three goes into 2 zero times remainder 2
2
>>> 0 % 3 What happens here? 3 goes into zero, zero times remainder 3
if we follow the logic of the previous two illustrations, that is not what was returned, zero was. Why?
>>> 0 % 3
0
The Python % operator is defined so that x % y == x - (x // y) * y, where x // y = ⌊x / y⌋. For positive integers, this corresponds to the usual notion of the “remainder” of a division. So, for any y ≠ 0,
0 % y
= 0 - ⌊0 / y⌋ * y by definition of %
= 0 - ⌊0⌋ * y because 0 divided by anything is 0
= 0 - 0 * y because 0 is an integer, so floor leaves it unchanged
= 0 - 0 because 0 times anything is 0
= 0
Look at it again:
1 % 3 is 0 remainder 1 => 1 = 3*0 + 1
2 % 3 is 0 remainder 2 => 2 = 3*0 + 2
0 % 3 is 0 remainder 0 [not 3] because 0 = 3*0 + 0
why are you taking what remains after the division in the first two cases but not the last?

List of all permutations

Verbs C. A. is related to permutations.
And they have very complicated documentation.
I want just get all possible permutations (n!)
For example for elements 1 2 3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Left argument of A. is a list of permutation indeces.
Right argument of A. is the list to be permuted.
The initial (unpermuted) list has index 0 and it goes on from there lexicographically [*].
Egs:
(0) A. 'a';'b';'c'
┌─┬─┬─┐
│a│b│c│
└─┴─┴─┘
(1 0) A. 1 2 3
1 3 2
1 2 3
(0 1 2) A. 5 1 2
5 1 2
5 2 1
1 5 2
To get all permutations of a list, you request all (! #y) (factorial of number of elements of list y to be permuted) of them, by requesting all indeces 0 ... (n-1): i. (! # y):
(i.!#y) A. y
[*]: Lexicographically by the implied list i. # y. That is, A. always permutes the simple list 0 ... n and then applies this permutation to your initial list: permutation { initial_list.

spoj - CPCRC1C, sum of digits of numbers 1 to n, need clarification, not solution

Once, one boy's teacher asked him to calculate the sum of numbers 1 through n.
the boy quickly answered, and his teacher made him another challenge. He asked him to calculate the sum of the digits of numbers 1 through n.
Input
Two space-separated integers 0 <= a <= b <= 109.
Output
The sum of the digits of numbers a through b.
Example
Input:
1 10
Output: 46
can someone explain what is meant by sum of the digits of numbers a to b?
from above, sum of {1 2 3 4 5 6 7 8 9 10 } is 55 , it is a well known Gaussian formula
but the output is 46!
if i count from 2 to 9, excluding the border numbers 1 and 10, the answer is 44 , still not 46
So what is meant by sum of digits of numbers?
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + (1 + 0)
Don't treat the 10 as the number 10, rather the digits 1 and 0

Resources