欧拉计划为什么说我得错了20号?

问题描述 投票:3回答:5

这是我试图解决的问题:Euler 20

n!意味着n ⨉ (n - 1) ⨉ ... ⨉ 3 ⨉ 2 ⨉ 1

例如,10! = 10 ⨉ 9 ⨉ ... ⨉ 3 ⨉ 2 ⨉ 1 = 362880010!数字的总和是3 + 6 + 2 + 8 + 8 + 0 + 0 = 27

找到数字100!中的数字总和

我试过这个:

y = 1  #The actual number
sum = 0 #The sum of its digits.

def factorial(x): #Calculates the number using a recursive factorial algorithm
    global y
    y = y*x
    if x > 1:
        factorial(x-1)
    else:
        return 0

factorial(100) #Calculate 100! and the function will put it in y.

def count(): #Calculates the sum.
    global y, sum
    while y > 0:
        currentDigit = int(y) % 10 #Find the remainder of the number gives the very last digit
        sum = sum + currentDigit #Add that to the total sum.
        y = int(y) / 10 #Divide y by 10 knocking of the 1s-digit place and putting the 10s digit in its place.
    return sum #return the sum.

print(count()) #print the count.

如果我做factorial(10)而不是100我得到27这就是我应该得到的问题,但我得到675为factorial(100)程序说错了。我做错了什么?我对Python并不熟悉,对不起,如果我犯了一个愚蠢的错误。

python
5个回答
5
投票

问题在于你实施count,特别是这一行:

    y = int(y) / 10

从Python 3开始,/就是true division。在Python 2.2及更高版本中,您可以使用from __future__ import division获得Python 3的除法行为。执行上面的行后,y是一个浮点数。大多数系统上的Python浮点数只有大约15个有效十进制数字(sys.float_info.dig将为您提供系统的精度;请注意,实际上有53位精度,等于53 / log2(10)≈15.955)。 count在那些重要的数字之后提取的任何十进制数字是舍入误差的结果,并且是将基数2浮点数转换为基数10的结果。在一定长度上,中间数字将不会对count计算的总和做出贡献。

[count(int('8' * 17 + str(k) + '0')) for k in range(1, 10)]
# [130, 130, 130, 130, 130, 130, 130, 130, 130]
import random
[count(int('8' * 17 + ('%05d' % random.randint(1,99999)) + '0')) for k in range(1, 10)]
# [146, 146, 146, 146, 146, 146, 146, 146, 146]

解决方案是使用//进行整数除法,此时您还可以删除int转换。当你在它的时候,你也可以将y作为count的参数,并使sum本地化。否则,您的全局sum变量将隐藏内置的sum函数。

至于在y中取消全球factorial,通过一个额外的论点很容易:

def factorial(x, p=1):
    p *= x
    if x > 1:
        return factorial(x-1, p)
    else:
        return p

这有tail-recursive的优势。标准python解释器不实现尾调用优化,但可以使用装饰器完成。将这样的装饰器应用于factorial,结果是一个不会导致堆栈溢出的函数。这种传递累积的技术可以用于许多其他函数以创建尾递归函数。

或者,编写一个迭代版本,这是一个更加pythonic。


4
投票

这是一个简单的方法来重写你的factorial()函数,使它不再依赖于全局变量:

>>> def fac(x):
...   if x > 1:
...     return x * fac(x-1)
...   else:
...     return 1
... 
>>> fac(10)
3628800
>>> fac(100)
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

它依赖于返回一个值并递归调用自己来执行计算。当然,计算100!意味着调用链将是100级深度,但为了清楚这一功能,我认为这种权衡非常值得。 (可能有更强大的方法来编写此函数 - 例如,更好地处理负数或非整数。)

有几种方法可以找到数字的总和 - 考虑将大数字看作单个字符串,迭代所有字符,并总结数字的整数值的方法和将数字视为数字的方法number,找到其十进制表示中最低位的值,并在您考虑它后删除该最低位。两种方法都有效。

至于你夏天的实际错误,我希望这是有教育意义的:

$ python3.2 -c "print (755 / 10)"
75.5
$ python2.7 -c "print (755 / 10)"
75

0
投票

找到数字100中的数字总和!

def sumFactorial(n):
    factorial=str(reduce(lambda x,y:x*y, range(n,0,-1))) 
    return sum(int(x) for x in factorial)

>>> sumFactorial(10)
27
>>> sumFactorial(100)
648
>>> 

0
投票
//In c++
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int main()
{
    int a[200];
    int carry=0,temp,m=1,k=0;
    int n;
    cin>>n;
    a[0]=1;
    for(int i=1;i<=n;i++)
    {
        k=0;
        for(int j=0;j<m;j++)
        {
            temp=a[j]*i+carry;  
            a[j]=temp%10;
            carry=temp/10;

        }
        while(carry!=0)
        a[m++]=carry%10,carry/=10;

    }
    long long ans=0;
    for(int i=m-1;i>=0;i--)
    {
    ans+=a[i];
    }
    cout<<endl;
    cout<<ans<<endl;
}
1.Array a[200] is used to store the digits of factorial.
2.Outer loop run from 1..100
3.varible m indicates no of digits in array or factorial
4.now lets take a example
  take a simple multiplication
  25 x 12
-------------
   5 0
 2 5
-------------
 3 0 0
300- value stores in temp;
by taking modulo 10 we can get the last digit it is placed array
300/10 becomes carry.

0
投票

我看到你使用了很多全局变量。

首先要做的事情:

我认为不是使用global y,而是可以在函数中使用y并返回值return y

而不是自己构建一个新功能,我想你可以使用built-in functions。请记住,使用内置函数比我们创建的函数更有效。我并不是说你创建的函数效率不高(实际上你创建的阶乘函数有一个非常有效的实现),但有时与我们创建的函数相比,内置函数的效率更高。

您可以从factorial function导入math module,而不是编写新的阶乘函数。

>>> from math import factorial

要将数字分成数字,您可以执行以下操作:

>>> a = list(str(factorial(100)))

正如您所看到的,我们正在将数字转换为字符串,将其分解,因此列表中包含字符串形式的数字。所以你应该再将它转换回int。要做到这一点:

>>> a = [int(x) for x in a]

我在上面的代码中使用了list comprehension

最后使用内置的sum function来获得解决方案。

>>> a = sum(a)
© www.soinside.com 2019 - 2024. All rights reserved.