How to find the GCD/HCF of three numbers in Python3 [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need a code to find the common prime factors of three numbers in Python 3
Whenever I run it shows me the wrong HCF / GCD.

Very simple.
Write a function, that calculates gcd/lcm of two numbers.
Then do something like this.
gcd(a,b,c) = gcd(a, gcd(b,c))
>>> def gcd(a,b):
... if b == 0:
... return a
... else:
... return gcd(b, a%b)
...
>>> gcd(3,5)
1
>>> gcd(10,5)
5
>>> gcd(10,15)
5
>>> gcd(5,gcd(10,15))
5
You can try by yourself, for lcm.

Related

Why is this code so much faster in Python 2 than Python 3? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 days ago.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
This is an algorithm for calculating the least number of perfect squares, that, when added, equal n
def numSquares(n):
square_nums = [i**2 for i in range(0, int(math.sqrt(n))+1)]
dp = [float('inf')] * (n+1)
dp[0] = 0
for i in range(1, n+1):
for square in square_nums:
if i < square:
break
dp[i] = min(dp[i], dp[i-square] + 1)
return dp[-1]
It runs pretty fast in Python 2, but horrendously slow in Python 3. Anyone know why?

check if a pin is long 4 or 6 digits or not [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
I just started learning py and i tried to create a simple program that checks whether a pin is long a 4 or 6 digits or not, but somehow it doesn't work.
def validate_pin(pin):
if len(pin) == 4 or len(pin) == 6:
return true
else:
return false
The value passed to validate_pin() would need to be of type str for this to make sense. Consider a PIN number of 0001. That's a valid PIN number but when expressed as int it isn't. Therefore:
def validate_pin(pin):
return len(pin) == 4 or len(pin) == 6
Python booleans are capitalized, so you have to change true to True and false to False

Why 0.123456789 == 12.3456789 / 100 is True? [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 last year.
Improve this question
I know the floating point issues and I've read some documentations and have some understanding on Decimal type.
For example: .1 + .1 + .1 != .3 .
But why:
>>> 0.123456789 == 12.3456789 / 100
True
is True ? I expected False.
Because adding is inaccurate so I think division should also be inaccurate?
Not really an answer, but too long for the comments:
To explore this phenomenon, I wrote a simple program:
import random
def f(a,b,n):
x = random.uniform(a,b)
y = n*x
return x,y,x == y/n
Then for example
trials = [f(0,100,100) for _ in range(10000)]
print(len([x for x,y,t in trials if t])/10000)
prints values like 0.8634
I have tried a number of values for a,b,n, with == holding typically in the range 80% to 90%. A strange observation which confuses me:
f(0,100,100000000)
is much more likely to give rise to a counterexample than either
f(0,100,10000000)
or
f(0,100,10000000000)
I would have thought that for a,b fixed, the probability of x==y is monotonic in n, but it apparently isn't.

Why does it say the printed output is longer? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a small program that prints the sum of numbers from 1 to 101 that are divisible by 5. I tried this and the output I get is just one line, but the site says the length of my printed output is 47, and it's longer than the instructor's printed output which is 4.
Don't print "the total sum of numbers divisible by 5 is". Just print the number. The checker doesn't like the English message.
As already pointed out, the sum of all integers divisible by 5 between 1 and 101 is 1050:
>>> def divisibles(start, end, divisor=5):
... for i in range(start, end+1): # Last value should be included
... if not i % divisor:
... yield i
...
>>> sum(divisibles(1, 101))
1050
>>>

Python Function Codecademy [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to cube the number in this function, but Codecademy says it isn't returning the right result. Could anyone help?
def cube(number):
return number**number
def by_three(number):
if number % 3==0:
return cube(number)
else:
return False
Because it is not a cube. Cube is: number ** 3
Given your cube function, you are doing
n^n
for example, given n = 4, what you are really doing is 4*4*4*4
And it work only on 3 or it's multiple, given the line
if number % 3==0:
In case you are interested in one liner of this function check this out:
def by_three(number):
return number ** 3 if number % 3 == 0 else False
If you are interested in how this is done check ternary operator in python

Categories