我正在尝试学习python,并尝试将js中的代码片段转换为python。我在js中创建了一个函数来计算GCD,如下所示:
// program to find the GCD of two integers
let gcd
function GCD() {
// take input
const number1 = prompt('Enter a first positive integer: ')
const number2 = prompt('Enter a second positive integer: ')
// looping from 1 t
我使用python中的以下算法实现了一个lcm问题。我的代码如下:
# Uses python3
import sys
def gcd_efficient(a, b):
#current_gcd = 1
#for d in range(2, min(a, b) + 1):
# if a % d == 0 and b % d == 0:
# if d > current_gcd:
# current_gcd = d
#return current_gcd
remainder = max(a
我对Python完全陌生。
我有一个简单的程序,找到两个数字的最大公分母(GCD)。它是这样的
def gcd(a, b):
if a == b:
return a
else:
if a > b:
return gcd(a-b, b)
else:
return gcd(a, b-a)
此文本当前在我的桌面上标题为gcd.py的记事本文档中。正如您所看到的,该程序实际上并不打印任何东西,它只是返回最大的公分母。
我需要通过执行以下print(gcd(25,10))命令来实际打印结果。我不知道我可以把这行放在哪里才能得到我
因此,在尝试将参数num1和num2传递给函数gcd时,我遇到了字符串格式化错误。我不知道该怎么解决这个问题。请容忍我,因为我是Python编程的新手。谢谢!
#!/usr/bin/python
import sys
from collections import defaultdict
lines = sys.stdin.read()
lineArray = lines.split()
listLength = len(lineArray)
def gcd(a, b):
c = 0
if a > b:
r = a%b
if r ==
在C语言中,这段代码可以工作,这里我在递归调用函数时没有使用返回。它给出了正确的输出
int gcd(int a, int b)
{
if(b == 0)
return a;
gcd(b, a % b);
}
但是,如果我在python中编写了相同的代码,那么这段代码将不返回任何内容(我认为值应该从If条件内的返回语句中返回)
def gcd(a, b):
if b == 0:
return a
gcd(b, a % b)
要使代码正常工作,我必须添加返回
def gcd(a, b):
if b == 0:
所以我用Python编写了一个程序来得到任意数量的GCD。
def GCD(numbers):
if numbers[-1] == 0:
return numbers[0]
# i'm stuck here, this is wrong
for i in range(len(numbers)-1):
print GCD([numbers[i+1], numbers[i] % numbers[i+1]])
print GCD(30, 40, 36)
该函数接受一个数字列表。但是,我不明白如何递归地使用该算法,以便它能够处理
我正在尝试使用Python3使用欧几里得算法来计算列表的gcd。但是,我得到了错误消息"TypeError: unsupported type(s) for %:'NoneType‘and 'int'“。我检查了所有地方,但仍然找不到我的代码有什么问题。这是我的代码,
# To get the gcd of a list
def gcd(numbers):
m = numbers[0]
for i in range(1, len(numbers)):
m = gcd2(m, numbers[i])
return m
#
这段Python代码也可以用Java代码编写吗?
def gcd(a, b):
# Return greatest common divisor using Euclid's Algorithm.
while b:
a, b = b, a % b
return a
print (gcd(210, 45))
这是我到目前为止在Java代码中所做的:
private static int gcd(int p, int q) {
// Return greatest common divisor using Euclid's Algo
这段python 3代码为我返回了None,而实际上它应该返回12,我搞不懂。
from math import *
def gcd(m,n):
print("gcd: ",m,", ",n,")")
if n == 0:
print(m)
return m
else:
gcd(n,m//n)
def main():
m = 60
n = 24
result = gcd(m,n)
print(result)
main()
def gcd_fast(a, b):
if(b > a):
a, b = b, a
if(a % b == 0):
return b
else:
rem = a % b
print('C:' + str(rem))
gcd_fast(b, rem)
print(gcd_fast(10, 9))
它返回none,当在return语句之后使用调试器运行它时,它会跳回到if子句中的gcd_fast(a, b)。我对python一点也不在行,如果这是件愚蠢的事情,
谁能解释一下为什么下面这段简单的代码(用欧几里得算法找出最大公分母)比Ruby中的同等代码慢3倍?
Iter_gcd.py的内容:
from sys import argv,stderr
def gcd(m, n):
if n > m:
m, n = n, m
while n != 0:
rem = m % n
m = n
n = rem
return m
# in Python3 code there is xrange replaced with range function
def main
我对Python的经验很少,我只是尝试创建一个函数,得到给定一个整数(列表中的元素数)和一个由正整数组成的列表中最大的公共除数
def generalGCD(num, arr):
if (num==2):
gcd(arr[0],arr[1])
else:
gcd(arr[0],generalGCD(num-1, arr[1::]))
def gcd(a, b) :
if (b == 0):
return a
else:
return gcd(b, a % b)
我目前还不熟悉Python,也不知道为什么会出现错误:
a<r raised exception RuntimeError: maximum recursion depth exceeded while calling a Python object
当我这么做时:
a = Rational(1,3)
r = Rational(0,5)
print(a<r)
我目前的代码是:
class Rational:
def _gcd(x,y):
while y != 0:
x, y = y, x % y
return x
我正在寻找一种有效的方法来确定两个python浮子的最大公共因子。该例程应该具有以下布局
gcd(a, b, rtol=1e-05, atol=1e-08)
"""
Returns the greatest common divisor of a and b
Parameters
----------
a,b : float
two floats for gcd
rtol, atol : float, optional
relative and absolute tolerance
Returns
-------
gcd : float
Gr
我正在研究python中的问题,它处理有理数,它有一个简化它的方法。例如,12/8给出了3/2。我做了这个问题,并得到了正确的答案,但我已经找到了分子和分母的gcd。当你说“Pythonic的方式”时,请有人帮助使用一些内置的特殊python特性或函数、模块或任何对python特有的东西!
是否有这样一种方式或任何测试用例应该包括在内,以涵盖所有的可能性?
这是我的代码:
class RationalNumber:
def __init__(self, n, d=1):
self.n=n
self.d=d
'''def gcd(self, a, b):
我试图在Python中列出数组中元素的所有公共因素,但无法使用gcd(),因为它只打印出最高的公共因子。有什么方法打印元素的所有因素吗?
from math import gcd
def solve(nums):
if len(nums) == 1:
return nums[0]
div = gcd(nums[0], nums[1])
if len(nums) == 2:
return div
for i in range(1, len(nums) - 1):
div = gcd(div, nums[i + 1])
我刚接触Python,
在初始化Fraction类的实例f之后,我希望已经调用了reduce方法,因此print结果是After reduced
f = Fraction(3,6)
print f #=> 1/2 not 3/6
代码如下:
class Fraction(object):
'''Define a fraction type'''
def __init__(self, num=0, denom=1):
'''Create a new Fraction with nume
我是python的新手,正在尝试创建一个python3脚本,使用欧几里得算法来查找GCD,但我总是遇到错误。
代码:
firstnum = input("Enter the first number: ")
secondnum = input("Enter the second number: ")
if firstnum == secondnum:
print("GCD is: {}").format(firstnum)
quit()
if firstnum > secondnum:
while True:
我目前正在学习Python,我在修改这个函数以按照我的意愿工作时遇到了问题。这是一个递归函数,从两个整数中查找gcd。data是一个包含两个整数的元组。如何修改这个递归函数以使用一个参数?
def gcd(data):
a, b = data
if b == 0: return a
return gcd(b, a % b)
如果我这样执行,我就会得到
TypeError: checkio() takes 1 positional argument but 2 were given
如果我试图通过定义def gcd(*data):来收集参数,我就会得到
ValueErro
我用Python写了同样的代码,用同样的逻辑,但是Kotlin没有显示我想要的值。
class LCMC {
fun gcd(a: Int, b: Int): Int {
if (b == 0) {
return a
}
return gcd(b, a % b)
}
fun lcm(n:Int) : Int {
var ans = 1
for (i in 1..n) {
var ans = (ans * i) / (gcd(ans, i))
我有一个可以工作的Django应用,叫做django_trial_on_mac,我最近把它移到了GitHub上,以便将来把它部署到heroku上。我将项目克隆到我的mac上的本地GitHub文件夹中,但当我从新目录中运行应用程序时,我收到以下错误:
Traceback (most recent call last):
File "/Users/danieloram/GitHub/Python_projects_for_github/django_trial_on_mac/manage.py", line 10, in <module>
e
我正在努力提高我的数学和编程技能,我的编程技能在婴儿和蹒跚学步阶段之间。我一直试图让Python分数模块中的gcd函数为我工作,它将工作在小整数上,但不适用于大整数。例如,如果我跑
from fractions import gcd
print gcd(37512, 84293)
如果我跑了
import fractions
a = 37512
b = 84293
cd = fractions.gcd(a, b)
print cd
在输出中,我得到了相同的内容:a。只有整数“1”。广泛的手工测试表明,这不应该是最大的共同点。这里出什么问题了?
我在python3.5中解决了问题。以下是我的解决方案代码:
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def lcm(a, b):
return int((a*b)/gcd(a,b))
def core():
N = int(input())
nums = [int(x) for x in input().split()]
ans = lcm(nums[0], nums[1])
for i in range(0, N):
for j in range(i+
我在Python中看到了gcd的这个实现:
def gcd(x,y): return y and gcd(y, x % y) or x
我不明白的是布尔值是如何在返回中工作的?在解释器中尝试了一些数字之后,我注意到and总是返回右边的数字,而or则返回左边的数字。为什么会这样呢?另外,你能一步一步地引导我完成这个函数的简单调用,这样我才能理解正在发生的事情吗?
我一直在努力学习算法,我必须做一个基本程序来使用python找到两个数字的gcd,所以我编写了以下代码
def gcd_better(m, n):
i = min(m, m)
while i:
if (m % i) == 0 and (n % i) == 0:
return i
else:
i = i - 1
if __name__ == "__main__":
gcd_better(4, 20)
在这里,我想返回我,但代码不是做的,谁能帮助我理解我的代码有什么问题吗
因此,我以前用Python进行了一些编程,并尝试使用一些我在那里使用的旧的解决方案/方法。但是我有一些语法/编译器错误,我觉得我遗漏了什么。可能是一些很蠢的东西,但这是密码。
//Euclidian Algorithm In C
//a=bq+r gcd(a,b)=gcd(b,r)
//Recursive Solution
//Devin Martin
#define _CRT_SECURE_NO_WARNINGS // to avoid scanf warning or error
#include <stdio.h>
int main()
{
int a, b
我有两段python代码,找到两个正整数的GCD。
以下是正确的答案:
def gcdRecur(a, b):
if b == 0:
return a
return gcdRecur(b, a%b)
下面是有bug的那个:
def gcdRecur(a, b):
a = max(a, b)
b = min(a, b)
if b == 0:
return a
return gcdRecur(b, a%b)
很容易看出这两段代码之间的区别。我知道没有必要再加上
a = max(a, b)
b = min(a,