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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:46:38+00:00 2026-05-26T22:46:38+00:00

Given two positive integers x and y, I need to find the next number

  • 0

Given two positive integers x and y, I need to find the next number greater than or equal to x that is a multiple of y.

For example:

x=18, y=3 => 18

or

x=18, y=5 => 20

or

x=121, y=25 => 125

My first thought was to keep incrementing x until I find a match but that can get fairly inefficient for high y values.

Then I thought about x - (x % y) + y but that wont work if x is a multiple of y. Of course, I can always adjust for that using a ternary operator in the formula x - ((x % y)==0?y:x % y) + y.

Does anyone have any good, clever, simple suggestions or a complete solution that is better than what I have touched on? Am I missing something obvious in my logic?

I’ll be using Java (this is a small piece of a greater algorithm), but pseudocode would be just as helpful if it is all straight math.

  • 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-26T22:46:39+00:00Added an answer on May 26, 2026 at 10:46 pm

    If x and y are positive ints then this will work:

    y * ((x-1)/y + 1);
    

    Using the x-1 allows you to not have to worry about the special case when x is a multiple of y. For example, if y = 5, then for 16 <= x <= 20,

    15 <= x-1 <= 19
    (x-1)/y == 3
    (x-1)/y+1 == 4
    y*((x-1)/y+1) == 20
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given two ranges of positive integers x: [1 ... n] and y: [1 ...
Given an infinite length sorted array having both positive and negative integers. Find an
Given two arrays, how to find the maximum element which is common to both
Given two sorted arrays of integers, a and b , and an integer c
Given two dates, what is the best method to calculate the number of days
Given two resources http://example.com/places/1 http://example.com/places/2 How should the endpoint look like to merge both
As a single operation between two positive integers we understand multiplying one of the
I had this question: Given an unsorted array of positive integers and an integer
Design an algorithm , FindElement(a,p), where a is two-dimensional square array of positive integers
I've two DateTime variable regardless of which is greater than the other in time.

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.