Page 1 of 2

Next Challenge!

Posted: Mon Nov 03, 2008 4:04 pm
by avansc
okay, here is a nice simple one.

remember when you did old addition
as in.

226
+74
----
300

well if you notice there were 2 carries.
so lets say for every carry, you assign a difficulty point.

your job is to take 2 numbers and tell me how many carries occured.

another example would be.

59216
+43115
---------
102331

there were 3 carries this time.

a full licenced version of VS2008 will be up for offer. the entry that solves the problem with the most innovative way wins. if there are 2 alike, i will hold a poll that will decide who wins.

edit : this progam can be done in 10 lines of code easily. probably less.

Re: Next Challenge!

Posted: Mon Nov 03, 2008 4:26 pm
by avansc
here is a hint. RECURSION

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:26 pm
by avansc
jeez, i must be more nerdy than i thought.

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:29 pm
by M_D_K
I'm working on it. It's just hard to think right now :)

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:42 pm
by Kleithap
avansc wrote:jeez, i must be more nerdy than i thought.
Heh, don't worry about it.

Here's a simple Java class that solves the problem:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10) {
			return n;
		} else {
			if ( ((p % 10) + (q % 10)) < 10)
				return numberCarries(p / 10, q / 10, n);
			else
				return numberCarries((11 + p) / 10, q / 10, n + 1);
		}
	}
}

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:45 pm
by avansc
i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:51 pm
by Kleithap
avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10)
			return n;
		else
			return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1);
	}
}

Re: Next Challenge!

Posted: Mon Nov 03, 2008 5:54 pm
by avansc
Kleithap wrote:
avansc wrote:jeez, i must be more nerdy than i thought.
Heh, don't worry about it.

Here's a simple Java class that solves the problem:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10) {
			return n;
		} else {
			if ( ((p % 10) + (q % 10)) < 10)
				return numberCarries(p / 10, q / 10, n);
			else
				return numberCarries((11 + p) / 10, q / 10, n + 1);
		}
	}
}
here is a common mistake in recursion.

Code: Select all

public static int numberCarries(int p, int q) {
      if ((p + q) < 10) {
         return 0;
      } else {
         if ( ((p % 10) + (q % 10)) < 10)
            return 0 + numberCarries(p / 10, q / 10, n);
         else
            return 1 + numberCarries((11 + p) / 10, q / 10);
      }
   }
pretty much the same solution i came up with, but did you test it? i think there are values that it wont work for.

Re: Next Challenge!

Posted: Mon Nov 03, 2008 6:01 pm
by avansc
Kleithap wrote:
avansc wrote:i'll post some hints. some things i used.
i used mod, with base 10. "num%10= remainer" - this is how you isolate the last digit in the number like 1234%10=4
i have 2 if statements.
and i use recursion.

armed with that you should probably come to the same function as me. its only about 6-7 lines in length
Well, yeah that's exactly what I did. If you want to make the function shorter you can always do this:

Code: Select all

public class Addition {
	public static void main(String[] args) {
		java.util.Scanner input = new java.util.Scanner(System.in);
		System.out.print("Enter first number: ");
		int p = input.nextInt();
		System.out.print("Enter second number: ");
		int q = input.nextInt();
		System.out.println("Carries of " + p + " + " + q +
				" = " + numberCarries(p, q, 0));
	}

	public static int numberCarries(int p, int q, int n) {
		if ((p + q) < 10)
			return n;
		else
			return ( ((p % 10) + (q % 10)) < 10) ? numberCarries(p / 10, q / 10, n) : numberCarries((11 + p) / 10, q / 10, n + 1);
	}
}
your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.

this little challange is trickier than you might think. and illustrates why you should allways test your code.

Re: Next Challenge!

Posted: Mon Nov 03, 2008 7:07 pm
by Kleithap
avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
Oops, you're right.

I've quickly made a simple one that uses an actual carry flag :D

Code: Select all

public static int numberCarries(int p, int q, int carry) {
      if ((p + q) + carry < 10)
         return 0;
      else
         return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1);
 }
It can probably be made more efficient but I don't have any time right now and I couldn't leave you without a solution :) . I didn't really have time to test this one, if you find any other bugs be sure to let me know. If I have time I'll look for a better solution tomorrow.
avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
I'm always happy when my sloppiness can be of use to other people.

Re: Next Challenge!

Posted: Mon Nov 03, 2008 7:10 pm
by avansc
Kleithap wrote:
avansc wrote:your code does not work, it produces 2 carries for the numbers 1901 and 99, it should be 3.
Oops, you're right.

I've quickly made a simple one that uses an actual carry flag :D

Code: Select all

public static int numberCarries(int p, int q, int carry) {
      if ((p + q) + carry < 10)
         return 0;
      else
         return ( ((p % 10) + (q % 10) + carry) < 10) ? 0 + numberCarries(p / 10, q / 10, 0) : 1 + numberCarries(p / 10, q / 10, 1);
 }
It can probably be made more efficient but I don't have any time right now and I couldn't leave you without a solution :) . I didn't really have time to test this one, if you find any other bugs be sure to let me know. If I have time I'll look for a better solution tomorrow.
avansc wrote:this little challange is trickier than you might think. and illustrates why you should allways test your code.
I'm always happy when my sloppiness can be of use to other people.
hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008

Re: Next Challenge!

Posted: Tue Nov 04, 2008 2:20 am
by Kleithap
avansc wrote:hahah, thats almost verbatim what i had.
Haha, great minds think alike :P

Seriously though, let everybody know when you think of another challenge, this one was pretty cool!

Re: Next Challenge!

Posted: Tue Nov 04, 2008 9:23 am
by avansc
Kleithap wrote:
avansc wrote:hahah, thats almost verbatim what i had.
Haha, great minds think alike :P

Seriously though, let everybody know when you think of another challenge, this one was pretty cool!
or is it "fools seldom differ"

Re: Next Challenge!

Posted: Tue Nov 04, 2008 9:27 am
by Kleithap
Well, yeah, that's probably more accurate.

Re: Next Challenge!

Posted: Wed Nov 05, 2008 7:33 am
by Kleithap
avansc wrote:hahah, thats almost verbatim what i had. nice work. unless someone gets something better you have yourself a copy of VS2008
Are you serious about VS2008? I mean it's really cool, but I don't live in the US so the costs sending me a copy are probably relatively high.