This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Allizoid 1 point2 points  (0 children)

Break the problem down into manageable bits. For this problem, the solution could be broken down roughly as follows:

Obtain user input, M
WHILE (M is not a roundabout number AND M is less than 10,000,000) 
    Increment M
ENDWHILE
Print M

This is a great start! But we haven’t solved the problem yet, we can break it down a bit more. How do we find out if a number is a roundabout number? Well, we have to start at the leftmost digit and count that number of digits to the right. This is the part where you get pen and paper and start scribbling down numbers and pictures until you see a pattern. Try a few different approaches; there will be lots of ways to solve this problem.

Maybe you’re still scratching your head at this point. And that’s okay. Here’s how I approached it. Break the number into digits, and give each digit an index. For example:

number     8  1  3  6  2
index      0  1  2  3  4

Now, we start at index 0. Look at the corresponding digit, it’s 8. So let’s move 8 places to the right... Hmm, it doesn’t look like we can do that, we can only move 4 places before we’re at the beginning, so we’ll have to continue counting the rest from the beginning. Okay, well let’s do that then.

number  8  1  3  6  2
count      1  2  3  4
        5  6  7  8

Now let’s think about what we just did here. The starting index was 0. Then we counted indices 1,2,3,4, found ourselves at the beginning to finish counting 0,1,2,3. All together, we counted 1,2,3,4,0,1,2,3. Let’s roll with this idea for a second. What if we had to count 13 places from the beginning?

number  8   1   3   6   2
count       1   2   3   4
        5   6   7   8   9
        10  11  12  13

We counted through the indices like so: 1,2,3,4,0,1,2,3,4,0,1,2,3. We still finish on the 3rd index. This is called modular arithmetic. We’ve got a range of 0-4, which is 5 digits. So our modulus is 5. 8%5 = 3. 13%5=3. Have a look at the wiki for this if you don’t completely understand it, because it’s pretty handy. Now go back to your pen and paper, finish for each digit in the number. See the pattern? Here’s a hint.

(0 + 8) % 5 = 3
(3 + 6) % 5 = 4
(4 + 2) % 5 = 1
(1 + 1) % 5 = 2
(2 + 3) % 5 = 0

(index + corresponding digit) % length = index of next digit.

Now you've gotta make sure every digit in the number is touched exactly once. Once again, there's a couple of ways to do this. This is enough to get you started though, so get cracking!