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 8506033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:29:04+00:00 2026-06-11T02:29:04+00:00

If a number is given as an input find sum of all the digits

  • 0

If a number is given as an input find sum of all the digits of number till that number

For example 11 is input then answer is 1+2….+9+(1+0)+(1+1)
The Brute-force method would be to calculate sum of digits of all the numbers that are less than a number.I have implemented that method iam wondering if there is any other way to do it without actually calculating sum of digits of every number

  • 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-06-11T02:29:06+00:00Added an answer on June 11, 2026 at 2:29 am

    You can do that faster (in O(log n) operations). Let S(n) be the sum of the digits of all numbers 0 <= k < n. Then

    S(10*n) = 10*S(n) + 45*n
    

    because among the numbers less than 10*n, each k < n appears as the initial part of a number 10 times, with last digits 0, 1, ..., 9. So that contributes 45 for the sum of the last digits, and 10 times the sum of the digits of k.

    Reversing that, we find

    S(n) = 10*S(n/10) + 45*(n/10) + (n%10)*DS(n/10) + (n%10)*((n%10)-1)/2
    

    where DS(k) is the plain digit sum of k. The first two terms come from the above, the remaining two come from the sum of the digits of n - n%10, ..., n - n%10 + (n%10 + 1).

    Start is S(n) = 0 for n <= 1.

    To include the upper bound, call it as S(n+1).

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

Sidebar

Related Questions

Given an arbitrary input string I'm meant to find the sum of all numbers
I am given an input, N, i have to find the number of list
Given a sorted list of numbers, I need to find the smallest number that
Am trying to find all prime numbers between two given numbers and sum the
Possible Duplicate: Python - How to check if input is a number (given that
Can anyone tell me how to format a given number of seconds (for example
How can I find the least prime number greater than a given number? For
I have developed an algorithm to find factors of a given number. Thus it
Possible Duplicate: How much time should it take to find the sum of all
Given a gray code for a number, find the binary code for the number.

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.