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.
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
The code below shall show a specific comment if the number of elements in the array isn't integer-type number.
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
How can I change the code to get 4%0.1==0?
import math
limits_real=(-2,2)
dr=0.1
if (limits_real[1]-limits_real[0])%dr!=0:
print ('Inapropriate limits or elements')
It works for dr=0.5 but not for dr=0.1 because print (4%0.1) returns 0.09999999999999978 instead of 0.
Because floating point numbers have limitations that every programmer should know, see this:
https://docs.python.org/3/tutorial/floatingpoint.html
As showed at the end of above documentation, you can use decimal module, which works exacly right but is slower than normal floating point arihtmetics:
from decimal import Decimal
limits_real=(-2,2)
dr = Decimal("0.1")
if (limits_real[1] - limits_real[0]) % dr != 0:
print ('Inapropriate limits or elements')
Note that you should use a str while constructing the Decimal instance, do not use a float.
I found out only this solution :) At least it works
import math
limits_real=(-2,2)
dr=0.1
if (((limits_real[1] - limits_real[0]) * 1000) % (dr * 1000)) / 1000 != 0:
print ('Inapropriate limits or elements')
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 3 years ago.
Improve this question
I already know how does abs function works.I mean it only shows you how far the number is from zero
The only thing i can't just get is with this example:
print(abs(3 + 4j)) # prints 5 !! Why ?
Because for complex numbers abs(number) will return magnitude of Complex Numbers.
Magnitude value will counted as :
√x2+y2 = √(3² + 4²) = √(9 + 16) = √(25) = 5.0
So abs with complex number will return magnitude of complex numbers.
for further reference you can use https://www.geeksforgeeks.org/abs-in-python/.
As the rest of the answers stated above, 3+4j is a complex number and the formula of calculating the absolute value of a complex number x+yi is sqrt( (x^2) + (y^2) ). In your case it's:
sqrt(3^2 + 4^2) = sqrt(9 + 16) = sqrt(25) = 5
Because in python, 3+4j is complex number and python calculates the absolute value or magnitude of the complex number when you do abs() on it. Magnitude of 3+4j is 5. Try this :
type(3+4j)
It should give <class 'complex'>.
Note : Magnitude of a complex number a+bj is ((a**2+b**2)**0.5)
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
Note: I cannot use any of the built in functions to solve this.
This is the question:
Given a function that returns a random integer number between 1 and 5, create a function that creates a random integer between 1 and 7.
As it has been answered in Adam Rosenfield's solution a while ago you may try to use an algoritm similar to this one below:
int i;
do
{
i = 5 * (rand5() - 1) + rand5(); // i is now uniformly random between 1 and 25
} while(i > 21);
// i is now uniformly random between 1 and 21
return i % 7 + 1; // result is now uniformly random between 1 and 7
EDIT: Translation to python:
def rand7():
i = 0;
while True:
i = 5 * (rand5() -1) + rand5()
if i > 21:
break
return i % 7 + 1
Apparently the following doesn't work. See the linked answer and the other responses as well.
Well, here is an approach I am thinking of. It could be wrong. I am going with the assumption that the result should be randomly distributed within the larger range.
Generate 7 random numbers using the given random functions (that generates numbers [1,5]) and calculate their sum.
This will result in a value between 7 (1 * 7) and 35 (5 * 7).
Because this value is evenly divisible by the target range and randomly distributed, then it seems like it would be valid to collapse the intermediate range [7,35] back to [1,7] without losing uniformity.
Or maybe that's not quite it - but it seems like exploiting a common multiple between the numbers is key.
import random
def winningFunc():
return originalFuncReturning1to5() + random.randint(0,2)