1

I need code or pointer for performing digit-wise addition. For example:

59 + 11 = 60 55 + 11 = 66 99 + 11 = 00 

Basically, I want to ignore carry when 9 + 1. So 9 + 1 should return 0 and not 10, and for any other digit it should return actual sum (i.e 5 + 1 = 6).

5
  • 1
    can you be a bit more precise about the rule? Commented Oct 21, 2011 at 3:29
  • 1
    I think he wants to rotate each digit individually through sequence [0-9]. Commented Oct 21, 2011 at 3:32
  • 21+11 = 32, 22+11 = 33, 29+11 = 30, 99+11=00 .... I will be always adding with 11, and largest number can be 99. Commented Oct 21, 2011 at 3:36
  • 3
    If this is homework, please add the homework tag. Commented Oct 21, 2011 at 3:37
  • It would be helpful if you told us the language. I don't want to code in Golfscript or Brainf***, and then realize you want it in Whitespace. Commented Oct 21, 2011 at 4:24

6 Answers 6

2

If you want to increment the digits individually

f(x) = (x/10 + 1) % 10 * 10 + (x % 10 + 1) % 10 

(Where % is the mod operator - it returns the remainder after division)

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, ObscureRobot, not it not typo. This is what I want. f(59) = 60 f(55) = 66 f(99) = 00
@PengOne yes, but it isn't clear that the asker needs more than two digits.
so much is unclear in that question! good solution for the 2 digit case
1

Use int digits = log10(x) to get the number of digits, then extract each digit x, replace with x + 1 % 10 and then put them back together, something like this:

int number = N; // STARTS AS THE ORIGINAL NUMBER int answer = 0; // WILL BE THE NEXT NUMBER int power = 1; // KEEPS TRACK OF POSITION int digits = log10(x); // TOTAL NUMBER OF DIGITS for (int d=0; d<digits; ++d) { int x = (number + 1) % 10; // GET NEXT DIGIT, INCREMENT IT answer += x*power; // ADD TO ANSWER IN CORRECT POSITION number = (number-x)/10; // REMOVE DIGIT FROM NUMBER power *= 10; // INCREMENT POSITION } 

Comments

0

To do this you need to extract the tens digit and ones digit seperately, add them seperately, then put them back together.

Here's an example: note that it isn't going to help you prevent the carries for the hundreds. For that you'd have to adapt the algorithm to handle it specifically, or split up the numbers by digits and add them that way.

int crazyAdd(int a, int b) { int aTens = a % 10; int bTens = b % 10; int tens = aTens + bTens; int ones = (a + b) % 10; return tens + ones; } 

Here's one that's more flexible

int crazyAdd(int a, int b) { int[] aDigits = extractDigits(a); // let there exist a function that int[] bDigits = extractDigits(b); // puts the digits into an array int size = aDigits.length; if(size < bDigits.length) size = bDigits.length; int digits = new int[size]; for(int i = 0; i < digits.length; i++) { int aDigit = i >= aDigits.length ? 0 : aDigits[i]; int bDigit = i >= bDigits.length ? 0 : bDigits[i]; digits[i] = (aDigit + bDigit) % 10; } int result = 0; for(int digit : digits) { result = result * 10 + digit; } return result; } 

Comments

0

I'm pretty sure it would be a pain in the ass mathematically, so the easiest would be to iterate through digits and rotate them. In Ruby:

def rotate_digits(n) result = 0 exp = 1 while n > 0 digit = n % 10 n /= 10 digit = (digit + 1) % 10 result += exp * digit exp *= 10 end result end puts rotate_digits(59) puts rotate_digits(55) puts rotate_digits(99) 

This gives you a number, so the last one gives you 0. If you really want "00", it's easier to work with strings:

def rotate_digits_as_string(n) n.to_s.each_char.map { |c| ((c.to_i + 1) % 10).to_s }.join end puts rotate_digits_as_string(59) puts rotate_digits_as_string(55) puts rotate_digits_as_string(99) 

Comments

0

If you're only talking about two-digit numbers, you can use a rather simple form:

def nextNum (num): val = int(num) if num == "99": return "00" if val > 89: return "0" + str(val - 89) if val % 10 == 9: return str (val + 1) return str (val + 11) 

Here's a little Python program showing that in action:

def nextNum (num): if num == "99": return "00" val = int(num) if val > 89: return "0%d"%(val - 89) if val % 10 == 9: return "%02d"%(val + 1) return "%02d"%(val + 11) for i in range (0,100): s = "%02d"%(i) print "%s -> %s"%(s,nextNum(s)) 

Comments

0

We need to do this for each character in the string input.

We have a function which will do this one character at a time.

char inc(char ch) { ch = (ch + 1) % '0'; // ANSI. return(ch); } 

Now we need a function that will do this to every character in the string:

string szinc(string input) { for(i = 0; i < input.size(); i = i + 1) { input[i] = inc(input[i]); } return(input); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.