"The Riddler Problem" Question

This is actually a question based entirely on, "Did I cheat?" and, "Did I do this efficiently?" Since I'm just self-study for learning working my way through another book, and I already solved the question. If you don't believe me, go ahead and throw the code that follows in and try it.

Primarily, based on the instructions of the question (given in the code) and keeping in mind:

My logic used to begin coding (also commented in the code)
How far along the material covers

Would you say I "cheated" by solving too much of the question BEFORE coding, and as the book to this point has only covered if, while, for, do-while, and switch statements, would you consider this GOOD and EFFICIENT code, i.e. the programming isn't far longer than it should/needs to be?

I'm sure there are better ways to do it, but it's important that it's taken in the context of what I'm supposed to know AT THIS MOMENT. There may well be a "code breaker" in the STL that would do this all in a single line for all I know.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "stdafx.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

/*
Holy digits Batman! The Riddler is planning his next caper somewhere on
Pennsylvania Avenue. In his usual sporting fashion, he has left the address
in the form of a puzzle. The address on Pennsylvania is a four-digit
number where:
• All four digits are different
• The digit in the thousands place is three times the digit in the tens place
• The number is odd
• The sum of the digits is 27
Write a program that uses a loop (or loops) to find the address where the
Riddler plans to strike.
*/
//My logic
//DIGITS: A B C D
//C max is 3
//B C D min is 7
//If A = 9, C = 3
//If A = 6, C = 2
//If A = 3, C = 1
//Sum A B C D = 27
//D is 7, 9 (odd number)
//B is 6, 7, 8, 9
//A must be 9 and C must be 3, as even if digits could repeat, no combination
//otherwise could equal 27. By same logic, no combination lower than B = 6
//and D = 7 appears to work either.

int main()
{
	int A{ 9 }, B{ 6 }, C{ 3 }, D{ 7 };

	for (B = 6; B <= 9; ++B)
	{
		if (A + B + C + D == 27 && A != B && A != C && A != D && B != C &&
			B != D && D != C)
		{
			cout << "ADDRESS: " << A << B << C << D << " Pennsylvania Ave.\n";
		}
	}
	D = 9;
	for (B = 6; B <= 9; ++B)
	{
		if (A + B + C + D == 27 && A != B && A != C && A != D && B != C &&
			B != D && D != C)
		{
			cout << "ADDRESS: " << A << B << C << D << " Pennsylvania Ave.\n";
		}
	}
	return 0;
}


As far as I can tell, the logic is sound in my initialization decisions, and I receive an answer that complies (9837). Would you consider this good and/or efficient for a beginner? Is there anything you would recommend? Just trying to learn best practices and efficient design.

Appreciate your time!
Well if you have learned functions yet then you could put the 'for' loop inside a function so that you're not having to repeat the same code. If not, then you've done would be fine for now. And you could have done it a little bit more dynamically by not doing any working out beforehand, this is the biggest improvement you could make. Obviously, this makes it more complicated in code to be able to write so you would have to use a couple of loops to increment each individual digit according to their rules and checking them in a more brute-force kind of way if you know what I mean. This would involve many nested 'for' loops, one for each digit. It can become a little bit of a mess that way so good practice would be to make it look as easy to understand as possible by using comments to describe the method you're using so it is easier to understand by 'outsiders'.
Last edited on
Thank you for your response, Tom!

In fact, I do know functions (I'm working through yet another book, since Stroustrup's started to get confusing past Chapter 5, and by 10 I felt I was missing things) but was trying to keep this exercise confined only to things I've been shown in the current book (Problem Solving With C++ 9e). There have been NUMEROUS exercises I've known of a far simpler way, but for the sake of not, "getting ahead of myself" refrained from using them.

And you could have done it a little bit more dynamically by not doing any working out beforehand, this is the biggest improvement you could make.


Honestly, that's exactly what I was thinking too, but then when I looked at it, I realized (as you said) it would have been an absolute MESS of nested loops. Even with good commenting it seems like something that would have been awful to read. Increment A by 1 each time to 9, then reset and do it with B, and so on, then repeat the process for every combination. At that point my thought process was, "It's just supposed to reinforce things I've learned, and this project could take hours to do it 'legit' in the sense that I iterate all possibilities." Of course it would have been easier to do this example by hand, and I did stop myself at some point for the sake of, "Better leave something to code," but I'm hoping what I left was sufficient for you and the community to think, "Yes, he seems to understand loops." :P
It is very clear to me from what you've written in code and as a response that you obviously understand loops very well. I also find you're not "getting ahead of myself" approach a wise and good decision to be taking in my humble opinion. However, as you've just said you realise there are better ways to be achieving this goal but at the moment with just using the aspects you've learned in your second book it is quite difficult to make an efficient and good way of achieving this without making it a complete mess. This is very clear and shows that you obviously know what you're talking about.

As you progress through your learning of C++ you will realise that there are so many different solutions to achieve your goal that there really isn't a "best" way of doing something as long as that method is an efficient one. People learn in different ways, personally I can't learn very well through reading it through a book and trying to do it unless it is written in such a specific way for me. I learn by watching other people do it and people showing me how to do it as they explain it. I like to watch video tutorials on the internet. I first started off not having a clue what I was doing and questioning literally everything to do with code. But as time passed and I practised and found the best way to learn I have slowly become a good programmer (at least in my opinion anyway!).

As you practice and learn you will make a very good programmer. I wish you luck, my friend.
Thank you very much sir! Also, I was about to rush back just to tell you that I was continuing study today, and for the first time I was properly introduced to pseudorandom number generation, and I thought of this topic and question, and how we both understood doing it the "hard" way would be most time consuming, then I thought, "I wonder if I could use random number generation to solve this quickly without 'cheating' the system too much and not spending hours on loops?" BEHOLD!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	int A, B, C, D;
	bool notFound = true;
	int count = 1;
	srand(time(0));
	while (notFound)
	{
		A = rand() % 10;
		B = rand() % 10;
		C = rand() % 10;
		D = rand() % 10;
		if (count % 1000 == 0)
		{
			cout << count << "th attempt.\n";
		}
		if ((A / 3 == C) && A != B && A != C && A != D && B != C &&
			B != D && D != C && (D == 1 || D == 3 || D == 5 || D == 7 || D == 9)
			&& (A + B + C + D == 27))
		{
			cout << "ADDRESS IS: " << A << B << C << D << " Pennsylvania Ave\n";
			cout << "Found on attempt " << count << endl;
			notFound = false;
		}
		++count;
	}
	return 0;
}


And it worked like a charm quickly, and with very little "fiddling around!" Thanks a million for the help, and the encouragement!
Like I said before, as you learn you find better ways to do things. You've proven me right just there and much quicker than I expected! Now you've opened my eyes up to that possible solution of this problem I've actually learned a little from you actually. I've been doing C++ programming for a couple of years and have moved onto more advanced and complicated techniques and aspects including graphical programming. After my time of practising my programming I've finally gotten near the stage I've always wanted to achieve and that's to make proper games. If you keep working on it and pushing yourself you can achieve anything.

Sometimes even the best programmers miss the obvious things or solutions, I know a couple of programmers that seem to overcomplicate things and make them much harder than they should be and as a result they end up with more inefficient programs and time wasted trying to do it.

Continue to push yourself and work your hardest and you'll achieve a lot in programming and life in general. In the words of Steve Jobs, "Stay Hungry, Stay Foolish" and Leonardo Da Vinci, "Simplicity is the ultimate sophistication".
Last edited on
Topic archived. No new replies allowed.