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 8 years ago.
Improve this question
For example:
x = (y*6) % 26 #this is a consistent formula
18 = (y*6) % 26
How would I solve for y? Or is there a way to reverse a % operation easily?
You can't really reverse modulo arithmetic like that. Modulo arithmetic returns basically equivalence groups. Think about a calendar...
Calendars ( and clocks ) are mod 12.
3 months after October is January, ie (10 + 3) % 12 == 1
15 months after October is January. ie (10 + 15) % 12 == 1
etc...
so your question seems to ask if you can tell what year it is by knowing what calendar month it is, and this is of course impossible
There is no exact way to reverse the operation as you have lost information as % only gives you the remainder when two numbers are divided.
It is like saying that if I divide x by 6 I get a whole number and a remainder of 3. I cant say what X is - it could be 9, it could be 15 or any of an infinite number of possibilities. To work it out you need the whole number as well as the modulus.
Why are you trying to do this - perhaps we can help if you tells us more of the problem you are trying to solve.
18 = (y*6) % 26
means that
y*6=n*26+18 where n can be any integer value. So
y=n* 26/6 + 3
or
y=n* 13/3 + 3
You may now say that y is an integer, too. This only happens for
y=n* 13 + 3
so, y= 3, 16, 29, 42, ...
Related
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 6 months ago.
Improve this question
Here in this program, I tried to understand but couldn't get completely.
How is this recursive function doing the sum and returning total sum of this? Please explain me in detail?
# Recursive Python3 program to
# find sum of digits of a number
# Function to check sum of
# digit using recursion
def sum_of_digit( n ):
if n < 10:
return n
return (n % 10 + sum_of_digit(n // 10)) # how this is working ?
num = 12345
result = sum_of_digit(num)
print("Sum of digits in",num,"is", result)
The best way to understand a recursive function is to dry run it.
First you need to understand what n % 10 mean is. this means the remainder of a n when divided by 10.
In this case when we divide 12345 by 10 , we get 5 as remainder.
so n % 10 part of code becomes 5.
Now, the second part is n//10 which gives you 1234 that are remaining digits.
Applying the same function again will give you 4 + sum_of_digit(123) and so on.
Even if this do not clear your confusion try, running this code on paper with some small number.
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 3 years ago.
I have just now started learning python from Learn Python 3 The Hard Way by Zed Shaw. In exercise 3 of the book, there was a problem to get the value of 100 - 25 * 3 % 4. The solution to this problem is already mentioned in the archives, in which the order preference is given to * and %(from left to right).
I made a problem on my own to get the value of 100 - 25 % 3 + 4. The answer in the output is 103.
I just wrote: print ("the value of", 100 - 25 % 3 + 4), which gave the output value 103.
If the % is given the preference 25 % 3 will give 3/4. Then how the answer is coming 103. Do I need to mention any float command or something?
I would like to know how can I use these operations. Is there any pre-defined rule to solve these kinds of problems?
Actually, the % operator gives you the REMAINDER of the operation.
Therefore, 25 % 3 returns 1, because 25 / 3 = 8 and the remainder of this operation is 1.
This way, your operation 100 - 25 % 3 + 4 is the same as 100 - 1 + 4 = 103
The % operator is used to find the remainder of a quotient. So 25 % 3 = 1 not 3/4.
From the docs you can find the full python operation order.
Then, % operator is called the modulus operator and return the remainder of the integuer division:
11 % 2 == 1
You may want to take a look at divmod
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 4 years ago.
Improve this question
The code is to find the factorial of each individual value in an array and then find the sum of them together .An example would be [1,2,3,4], which will then be 1!+2!++3!+4!=33.A single integer equal to the desired sum, reduced modulo . The issue is that when it comes on to large numbers(just my assumption) it results in "Terminated due to timeout" status
At first I used a for loop to go through each value in the array. Thinking that it may be a search issue I used for in range to ensure it has a set range. Sadly that still hasn't solved the problem. I now assume that it has to be a problem with factorial since multiplication is
def factModSum(arr):
sum=0
for i in range (0,len(arr)):
sum=sum+factorial(arr[i])
return sum%(10**9)
Example 1:
Input: 1 2 3 4
output: 33
Expected output: 33
Example 2:
Input:2 3 5 7
Output:5168
Expected output: 33
Example 3:
Input:12 13 14
Output:884313600
Expected output: 33
At the core of it at the function works. But Im getting timeout error for some of my Test case , therefore assuming that the code is not able to process large numbers in a given time
If you are modding by 10 ** 9 you can try this:
def factModSum(arr):
return sum(factorial(i) for i in arr if i < 40) % 10**9
This is because n! with n >= 40 is congruent to 0 mod 10**9.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.
Is anybody able to guide me through this, without spoiling it for me?
There are two things you can do. The "fast" way (a la the story about the young Gauss) recognizes that
sum(1:N) = N * (N + 1) / 2
But I doubt that is what is asked.
You need to create a loop (look at the for command) over a range (look at the range command), and in each iteration add the current value of the loop variable to the sum (which you initialize to zero before the start of the loop).
There - you should now be OK.
EDIT with a while loop, and still leaving you to do a little bit of work:
mySum = 0
i = 1;
while( <<< put some condition here >>> ):
mySum = mySum + i
<<<<< do something clever with i >>>>>
print <<<<< what do you think you should print here? >>>>>
Note that indentation is important in Python, and the : at the end of the while statement matters
This question already has answers here:
What is the result of % in Python?
(20 answers)
Closed 9 years ago.
I'm working on the exercises in Learning Python the Hard Way and I am wondering what the % function does. I am getting the wrong answer when i count my eggs and hoping understanding this will help me understand what I'm doing wrong.
I can't really tell why your code is broken because you haven't shown anybody what your code is. Please post samples and links next time.
Python % is used in two places, one is mathematical (the modulo operator), and the other has to do with formatting text. I'm going to assume "counting eggs" means the math way.
The modulo operator in X % Y means "Divide X by Y and give me the remainder." So:
10 % 2 == 0
10 % 3 == 1
10 % 11 == 10
That is the the modulo operator