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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:57:25+00:00 2026-05-17T06:57:25+00:00

Suppose I have an int x = 54897 , old digit index (0 based),

  • 0

Suppose I have an int x = 54897, old digit index (0 based), and the new value for that digit. What’s the fastest way to get the new value?

Example

x = 54897
index = 3
value = 2
y = f(x, index, value) // => 54827

Edit: by fastest, I definitely mean faster performance. No string processing.

  • 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-17T06:57:26+00:00Added an answer on May 17, 2026 at 6:57 am

    In simplest case (considering the digits are numbered from LSB to MSB, the first one being 0) AND knowing the old digit, we could do as simple as that:

    num += (new_digit - old_digit) * 10**pos;
    

    For the real problem we would need:

    1) the MSB-first version of the pos, that could cost you a log() or at most log10(MAX_INT) divisions by ten (could be improved using binary search).

    2) the digit from that pos that would need at most 2 divisions (or zero, using results from step 1).

    You could also use the special fpu instruction from x86 that is able to save a float in BCD (I have no idea how slow it is).

    UPDATE: the first step could be done even faster, without any divisions, with a binary search like this:

    int my_log10(unsigned short n){
        // short: 0.. 64k -> 1.. 5 digits 
        if (n < 1000){  // 1..3
            if (n <  10) return 1;
            if (n < 100) return 2;
            return 3;
        } else { // 4..5
            if (n < 10000) return 4;
            return 5;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have: public class Bob { public int Value { get; set; }
Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int
suppose I have a type defined as type value = None | Int of
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
Suppose I have an int-array and I want to modify it. I know that
Suppose I have: class Foo { public int Bar { get; set; } }
Suppose we have an array say: int arr[1000]; and I have a function that
Suppose I have a code as follows: int Main() { if (true) { new
Suppose I have a method public Patient(int id) { ---- } that returns Patient
suppose we have these classes: public class Record { int key; int value; Record(){

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.