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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:33:42+00:00 2026-06-08T13:33:42+00:00

I need to find the first multiple for a number starting from a base

  • 0

I need to find the first multiple for a number starting from a base number. For example: The first multiple of 3 from 7 is 9. My first attempt was to do this:

multiple = baseNumber
while(multiple%number !=0 )
    multiple++

At the end, “multiple” will have the first multiple of number after baseNumber. The problem is that when number becomes too large, the number of iterations becomes too many. So my question is: is there a faster way to do this?

  • 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-08T13:33:45+00:00Added an answer on June 8, 2026 at 1:33 pm

    If everything is guaranteed to be positive, try

    multiple = baseNumber + number - 1;
    multiple -= (multiple % number);
    

    That does it in constant time.

    First, we add number - 1 to make sure that we have a number at least as large as the next multiple but smaller than the one after that. Then we subtract the remainder of the division by number to make sure we have the desired multiple.

    If baseNumber can be negative (but number still positive), we face the problem that multiple % number may be negative if multiple < 0, so the above could skip a multiple of number. To avoid that, we can use e.g.

    remainder = multiple % number;
    if (remainder < 0) remainder += number;
    multiple -= remainder;
    

    If branching is too expensive, we can avoid the if at the cost of two divisions instead of one,

    multiple -= (number + (multiple % number)) % number;
    

    Generally, the if seems preferable, though.

    If number can be negative, replace it with its absolute value first.

    Note: The above returns, as the original code does, baseNumber if that is already a multiple of number. If that isn’t desired, remove the - 1 in the first line.

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

Sidebar

Related Questions

Need to find the timestamp for the first minute of the first day of
To find strongly connected components in a directed graph G you need to first
I need to find an elegant way to do 2 kinds of MixIns. First:
All, I'm pretty new with JavaScript and this is my first attempt with the
I need some help with query from multiple tables. My database: I have following
Given two positive integers x and y, I need to find the next number
Given an integer array, i need to find which number occurred most number of
I need to find a flexible solution for access control in PHP. In the
i need to find out automationid for key board values? how to send keystrokes
I need to find all the occurences of a file Test.TXT in c:\ or

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.