Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:51:35+00:00 2026-05-27T00:51:35+00:00

Here is the problem I am trying to solve: Euler 20 . n! means

  • 0

Here is the problem I am trying to solve: Euler 20.

n! means n ⨉ (n - 1) ⨉ ... ⨉ 3 ⨉ 2 ⨉ 1

For example, 10! = 10 ⨉ 9 ⨉ ... ⨉ 3 ⨉ 2 ⨉ 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

I have tried this:

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.

If I do factorial(10) instead of 100 I get 27 which is what the problem says I should get, but I get 675 for factorial(100) which the program says is wrong. What did I do wrong? I am not that familiar with Python, sorry if I made a stupid error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:51:36+00:00Added an answer on May 27, 2026 at 12:51 am

    The problem lies in your implementation of count, specifically the line:

        y = int(y) / 10
    

    Since Python 3, / is true division. In Python 2.2 and later, you can get Python 3’s division behavior with from __future__ import division. After executing the above line, y is a float. Python floats on most systems only have about 15 significant decimal digits (sys.float_info.dig will give you the precision for your system; note that there are actually 53 bits of precision, which is equal to 53 / log2(10) ≈ 15.955). Any decimal digit that count extracts after those significant ones is the result of rounding error and is a consequence of converting a base-2 float into base 10. Over a certain length, the middle digits won’t contribute to the sum calculated by 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]
    

    The solution is to use // for integer division, at which point you can also remove the int conversions. While you’re at it, you might as well make y a parameter to count, and make sum local. Otherwise, your global sum variable will hide the built-in sum function.

    As for doing away with the global y in factorial, it’s quite easy to pass an extra argument:

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

    This has the advantage of being tail-recursive. The standard python interpreter doesn’t implement the tail call optimization, but it can be done with a decorator. Apply such a decorator to factorial and the result is a function that won’t cause a stack overflow. This technique of passing an accumulation can be used for many other functions to create a tail-recursive function.

    Alternatively, write an iterative version, which is a little more pythonic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a problem I am trying to solve: I have an irregular shape.
I have an interesting problem here I've been trying to solve for the last
Here is the problem I'm trying to solve for my game. I have this
Alright, here is my problem i'm trying to solve. I have an index page
Here is the problem I am trying to solve. I have a table where
Here's the problem I'm trying to solve: I have a dynamic php-driven website that
EDIT Here is the problem I am trying to solve: I have a string
I have a problem here which i am trying to solve. The program is
Here's a fictitious example of the problem I'm trying to solve. If I'm working
Here's the coding problem I am trying to solve... I have a base class,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.