Using the (x%y) in python [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Why in python the x%y result is x when x is less than y. should the result be zero?
for example when I try 4%5, python give me 4 as a result of and I believe it should be zero

% -> refers to the modulo operator
So you would get the remainder if you use this operator
4%5 will give 4 as 4 is the remainder when you divide 5 by 4
If you wish to get the quotient then you can use // which is used for absolute division
so 4%5 gives 4
and 4//5 gives 0

The % symbol in Python is called the Modulo Operator.
It returns the remainder of dividing the left hand operand by right hand operand. It's used to get the remainder of a division problem.
Example:
5%6
# results in 5
See the long division below
_____0__
6 | 5
| 0
|_____
| 5
Performing 5 / 6 results in reminder of 5, thus 5%6 is 5

Related

calculate the digits of the Nth number using the given numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I'd like to get the digits of the Nth number by using the given numbers which are 3 and 9. From these numbers, I've got the numbers like 3, 9, 33, 39, 93, 99, 333 ... in order. So the digits of the 4th number is 2 and that of the 7th number is 3. What I want to do is to calculate the digits of the Nth number from this logic. Any suggestions? Thanks.
(You may assume that N is 1000000)
+What I've found was that there are 2 1-digit numbers (=2^1), 4 2-digit numbers (=2^2), and 8 3-digit numbers(=2^3). So I tried to apply the concept of geometric sequence but wasn't able to make it.
Written in binary, there are 10 numbers of a single digit, 110 numbers of two digits or less, 1110 numbers of three digits or less, 11110 numbers of four digits or less... So (2 << k)-2 numbers of k digits or less.
For a given N, it is an easy matter to find the smallest k that fits.
You already know that quantity of numbers with length k is 2^k, so we can exploit this fact. Essentially this is implementation of int(log2(n+1)) without any math libraries:
def nlen(n):
lng = 0
lcnt = 1
overall = 0
while overall < n:
lcnt *= 2
overall += lcnt
lng += 1
return lng

perform arithmetic operations python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
given the number 12345 = 1x10^4 + 2x10^3 + 3x10^2 + 4x10^1 + 5x10^0, how to perform some arithmetic operations to leave you with just the digit at position 5 (from the left) and output it to the screen? Thanks for the help!
Assuming you want to stick to arithmetic operations (and not strings), use the modulo operator with 10 to get the remainder of division by 10, i.e. the unit:
12345%10
output: 5
For an arbitrary number, you need to compute the position, you can use log10 and ceil:
from math import log10, ceil
N = 5
number = 1234567
number//10**(ceil(log10(number))-N)%10
output: 5

How do i figure out the multiple of a number python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
ok, I am studying python in my computing course and i have been challenged with designing a code which validates a GTIN-8 code, I have came to a hurdle that i can not jump. I have looked on how to find the equal or higher multiple of a 10 and i have had no success so far, hope you guys can help me!
Here is a small piece of code, i need to find the equal or higher multiple of 10;
NewNumber = (NewGtin_1 + Gtin_2 + NewGtin_3 + Gtin_4 + NewGtin_5 + Gtin_6 + NewGtin_7)
print (NewNumber)
The easiest way, involving no functions and modules, could be using floor division operator //.
def neareast_higher_multiple_10(number):
return ((number // 10) + 1) * 10
Examples of usage:
>>> neareast_higher_multiple_10(15)
20
>>> neareast_higher_multiple_10(21)
30
>>> neareast_higher_multiple_10(20.1)
30.0
>>> neareast_higher_multiple_10(20)
30
We can also make a generalized version:
def neareast_higher_multiple(number, mult_of):
return ((number // mult_of) + 1) * mult_of
If you need nearest lower multiple, just remove + 1:
def neareast_lower_multiple(number, mult_of):
return (number // mult_of) * mult_of
To find the nearest multiple, you can call both these functions and use that one with a lower difference from the original number.

A puzzle from collision resolution formula in python source code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I feel so sorry that asked such problem here even though it looks somewhat like a mathematic issue.
The following collision resolution formula appeared in python source code, \pythoncore\Objects\dictobject.c
j = ((5*j) + 1) mod 2**i
The brief description for this formula is For any initial j in range(2*i), repeating that 2*i times generates each
int in range(2*i) exactly once. In an
example that's really too small to make this entirely clear, for a table of
size 2*3 the order of indices is:
0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating]
My question is how to prove the correctness of such formula.
The comment you're referring to contains the instruction
see any text on random-number generation for proof
If you search a bit, you'll find that the 5*j+1 mod 2**i recurrence is a linear congruential generator. A linear congruential generator is of the form
x_(n+1) = a*x_n + c (mod m)
For nonzero c, a linear congruential generator has full period (generating all numbers mod m) if and only if
c and m are relatively prime,
a-1 is divisible by all prime factors of m, and
if m is a multiple of 4, a-1 is also a multiple of 4.
This is known as the Hull-Dobell theorem. All these conditions hold for 5*j+1 mod 2**i, so the recurrence goes through all entries in the hash table.
A full proof of the Hull-Dobell theorem can be found here.

Order of precedence equation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Evening all, I am more or less familiar with the order of precedence but this one confuses me and I have an exam tomorrow so yer :d....
Ok so, 6-2/2+5
Is it:
2/2 = 1
1+5 = 6
6-2 = 4
then 4+6 = 10
The part that confused me is once you have in step 3 the value 4 then you just add what ever is left in the equation to the total ?
(2/2) = 1 division is performed first
(6-(1)) = 5 + and - have equal precedence, so we go left to right, hence 6 - (2/2) is done first
(5)+5 = 10 finish it by doing addition
No, it is:
6-2/2+5
= 6 - 2/2 + 5
= (6 - (2/2)) + 5
= (6 - 1 ) + 5
= 5 + 5
= 10
/ and * before + and -. In case of equal precedence it evaluates from left to right.
For more details you might want to consider the documentation (just scroll down to the end).

Categories

Resources