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 7 months ago.
Improve this question
i have been coding in python for some time now, and am trying to make a robotic hand in combination with Arduino. I have 2 servo's facing the opposite way (small electric motors) which means that when i rotate them by the same angle, the second one will rotate the opposite way. As the servo can't handle negative integers, i have to come with a solution that basically "reverses" the integer. What i mean by that is that if the number is greater than the middle point (45 in this case) i want it to be smaller, so lets say we have 46 it should be 44 and 47 -> 43 so on. How would i go about creating this? Thanks for reading.
This is just a tricky algorithm:
edge = 45
number = 44
result = edge + (edge - number)
result will be 46
edge = 45
number = 47
result = edge + (edge - number)
result will be 43
Related
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
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm trying to make a Pi calculator in python but I need more decimal places.
it would help a lot if someone edited my code and carefully explained what they did.
this is the code I'm using.
import math
d = 0
ans = 0
display = 0
while True:
display += 1
d += 1
ans += 1/d**2
if display == 1000000:
print(math.sqrt(ans*6))
display = 0
# displays value calculated every 1m iterations
output after ~85m iterations: (3.14159264498239)
I need more than 15 decimal places (3.14159264498239........)
You’re using a very slowly converging series for π²∕6, so you are not going to get a very precise value this way. Floating point limitations prevent further progress after 3.14159264498239, but you’re not going to get much further in any reasonable amount of time, anyway. You can get around these issues by some combination of
micro-optimising your code,
storing a list of values, reversing it and using math.fsum,
using decimal.Decimal,
using a better series (like this one),
using a method that converges to the value of π quickly, instead of a series (like this one),
using PyPy, or a faster language than Python,
from math import pi.
you could try with a generator:
def oddnumbers():
n = 1
while True:
yield n
n += 2
def pi_series():
odds = oddnumbers()
approximation = 0
while True:
approximation += (4 / next(odds))
yield approximation
approximation -= (4 / next(odds))
yield approximation
approx_pi = pi_series()
for x in range(10000000):
print(next(approx_pi))
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 2 years ago.
Improve this question
I am trying to mimic a chance encounter and I'm not sure how to do this. I would like to use a poisson distribution, but am open to other suggestions.
The idea is this: there is a central place where you can meet people. If there are 10 (the minimum) people at this place, the chance is 100% you will meet a specific person (e.g., chance = 1). If there are 6000 (the maximum) people at this place, the chance you will meet everyone is 3.5% you will meet a specific person (e.g. Chance = 0.035). How can I implement this using Python?
Not exactly sure if you can do Poisson distribution without μ.
This function will return a chance based on n, which is the number of people, and the given maximum and minimum values. In this calculation, the probability will be halved for each 1 point increase in the normalized value up to a certain point which is log(0.035)/log(0.5) or about 4.83650 because 0.5 ** 4.83650 ≈ 0.035. Meaning whenever n = maximum, the probability will be 0.035 and whenever n = minimum, the probability will be 1.
import math
def chance(n): # n is the number of people
maximum = 6000
minimum = 10
normalized = ((n - minimum) / (maximum - minimum)) * (math.log(0.035) / math.log(0.5))
probability = 0.5 ** normalized
return probability
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.
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.