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

The Archive Base Latest Questions

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

if we have 2 numbers, say a and b then how can we find

  • 0

if we have 2 numbers, say a and b then how can we find the value of sum of b%i where i ranges from 1 to a?
One way is to iterate through all values from 1 to a but is there any efficient method?
(better than O(n) ?)
E.g : if a = 4 and b = 5 then required ans = 5%1+5%2+5%3+5%4=4
Thanks.

  • 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-30T00:24:54+00:00Added an answer on May 30, 2026 at 12:24 am

    For i > b, we have b % i == b, so that part of the sum is easily calculated in constant time ((a-b)*b, if a >= b, 0 otherwise).

    The part for i <= b remains to be calculated (i == b gives 0, thus may be ignored). You can do that in O(sqrt(b)) steps,

    • For i <= sqrt(b), calculate b % i and add to sum
    • For i > sqrt(b), let k = floor(b/i), then b % i == b - k*i, and k < sqrt(b). So for k = 1 to ceiling(sqrt(b))-1, let hi = floor(b/k) and lo = floor(b/(k+1)). There are hi - lo numbers i such that k*i <= b < (k+1)*i, the sum of b % i for them is sum_{ lo < i <= hi } (b - k*i) = (hi - lo)*b - k*(hi-lo)*(hi+lo+1)/2.

    If a <= sqrt(b), only the first bullet applies, stopping at a. If sqrt(b) < a < b, in the second bullet, run from k = floor(b/a) to ceiling(sqrt(b))-1 and adjust the upper limit for the smallest k to a.

    Overall complexity O(min(a,sqrt(b))).

    Code (C):

    #include <stdlib.h>
    #include <stdio.h>
    #include <math.h>
    
    unsigned long long usqrt(unsigned long long n);
    unsigned long long modSum(unsigned long long a, unsigned long long b);
    
    int main(int argc, char *argv[]){
        unsigned long long a, b;
        b = (argc > 1) ? strtoull(argv[argc-1],NULL,0) : 10000;
        a = (argc > 2) ? strtoull(argv[1],NULL,0) : b;
        printf("Sum of moduli %llu %% i for 1 <= i <= %llu: %llu\n",b,a,modSum(a,b));
        return EXIT_SUCCESS;
    }
    
    unsigned long long usqrt(unsigned long long n){
        unsigned long long r = (unsigned long long)sqrt(n);
        while(r*r > n) --r;
        while(r*(r+2) < n) ++r;
        return r;
    }
    
    unsigned long long modSum(unsigned long long a, unsigned long long b){
        if (a < 2 || b == 0){
            return 0;
        }
        unsigned long long sum = 0, i, l, u, r = usqrt(b);
        if (b < a){
            sum += (a-b)*b;
        }
        u = (a < r) ? a : r;
        for(i = 2; i <= u; ++i){
            sum += b%i;
        }
        if (r < a){
            u = (a < b) ? a : (b-1);
            i = b/u;
            l = b/(i+1);
            do{
                sum += (u-l)*b;
                sum -= i*(u-l)*(u+l+1)/2;
                ++i;
                u = l;
                l = b/(i+1);
            }while(u > r);
        }
        return sum;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of numbers ( integers ) (say, from 1 to 10).
What is the best way to get formatted Int32 numbers? Let say I have
I have a listbox where users can enter decimal numbers. Let's say they'll enter
Lets say I have a server program that can accept connections from 10 (or
Say I have a string. Then I have a number of unique tokens or
I have a array of integer numbers (say 1,2,3,4,5 or 1,2,3, ... 10 or
Lets say I have an array numbers that contains the following values: int numbers
I have the following numbers : 1,2,3,4,5,6,7,8,9,10,11,12... I have a number n, say n=2
Let's say you have a random number generator spitting out numbers between 1 and
Let's say I have a QTableView with a corresponding model. This view shows numbers

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.