Cycles and stuff - What does this program do?

0

1

I just found one of my first C++ programs in some deep caves on my disk...

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int     a , b , c , d , e , f , g , h , i , j , x , n , N , S;
enum    cycle { A , B , C };
string  Str_;
cycle Cy;

void next_Cy();

int main() {
S = 0;
Cy = A;     // Set first cycle

cout << "Enter n:\n";

getline ( cin, Str_ );
stringstream (Str_) >> x; // take x

if ( x >= 10 ) {
    n  = (x / 10)-1;    // defining n

    for ( N = 0; N <= n; N++ ) {
        a  = N*10+0;        // defining the digits
        b  = N*10+1;
        c  = N*10+2;
        d  = N*10+3;
        e  = N*10+4;
        f  = N*10+5;
        g  = N*10+6;
        h  = N*10+7;
        i  = N*10+8;
        j  = N*10+9;

        switch ( Cy ) {          // switch cycle cases
            case A:
                S += a + d + f + g + j;
                break;
            case B:
                S += a + c + f + i;
                break;
            case C:
                S += a + b + e + f + h;
                break;
            }
            next_Cy();          // next cycle ..
        }
        cout << S << endl;
    }
return 0;
}

// function to define next cycle
// A becomes B
// B becomes C
// C becomes A again ,,
void next_Cy() {
    if ( Cy == A ) {
        Cy = B;
    }
    else {
        if ( Cy == B ) {
            Cy = C;
        }
        else {
            Cy = A;
        }
    }
}

(I can't believe that I wrote this shit some day in my life.)

CHALLENGE: Try to find out what this program does. (Yes, it does something "useful".)

Compiling and executing the code is of course allowed. (Yes, it does compile and run.)

% g++ puzzle.cpp -o puzzle
% ./puzzle
Enter n:
9000
18895500
%

So basically, you just enter a number (which should be greater than 9 because reasons) and then you will get some result, whatever that is.

By the way, I didn't modify the code or anything, these variables were really labeled like that.

PS: My favorite comment is // take x.

The first to find out what this code does, wins.

The explanation having the most upvotes wins.

Use the spoil tag on your answer!

YouniS Bensalah

Posted 2015-01-09T13:51:01.780

Reputation: 27

Question was closed 2015-01-09T15:13:44.170

I believe that this kind of question is more suited for Stack Overflow – Optimizer – 2015-01-09T13:52:18.970

1Is this really a programming puzzle or code golf? – KSFT – 2015-01-09T13:54:31.833

@Optimizer, Humm, but SO is for "serious" programming problems, not for puzzles et. al. I already know what this code does. I just thought it might be fun to figure out. Please pardon my misplaced post. – YouniS Bensalah – 2015-01-09T13:59:16.180

@younishd your's is not really a puzzle as "the first to find wins" is not a criteria for puzzles, but for debugging. If you can change the winning criteria which gives more users a shot at this , then it will be called a puzzle/challenge. – Optimizer – 2015-01-09T14:03:48.310

1@KSFT: I do not think this question is appropriate for Stack Overflow, but we could ponder this as a new type of challenge. I am not certain whether this would be a good challenge though (with the caveat that I have not yet looked at the program in this question, which may be a first good example). – Wrzlprmft – 2015-01-09T14:05:17.320

@Optimizer, I'm okay with that, but how would you formulate the winning criteria? – YouniS Bensalah – 2015-01-09T14:09:29.743

@Optimizer, like this? – YouniS Bensalah – 2015-01-09T14:13:07.687

I have no idea :) – Optimizer – 2015-01-09T14:13:11.787

@KSFT, thanks for being open. – YouniS Bensalah – 2015-01-09T14:13:24.203

@Optimizer, here's another idea for this to become a "real puzzle": find out what the program does (without spoiling it) and write a similar program that is even more complex/obfuscated/bizarre. – YouniS Bensalah – 2015-01-09T14:17:23.860

1That should work if you can define what is more complex/... – Optimizer – 2015-01-09T14:19:32.313

1You should state that all answers should be in spoiler tags so as not to take away the challenge for those who want to figure it out on their own. – Jordon Biondo – 2015-01-09T14:58:06.433

I think this is a good idea for a new type of puzzle. We should have a new tag for this kind of puzzle. – KSFT – 2015-01-09T15:08:36.920

@JordonBiondo, done. – YouniS Bensalah – 2015-01-09T15:09:59.197

@KSFT code-riddle or code-guess ? – YouniS Bensalah – 2015-01-09T15:19:07.007

@KSFT (cc younishd) I think we should discuss this on meta (both if this is a good fit for PPCG, and we could also vote on a tag, if it is) – Martin Ender – 2015-01-09T15:29:57.213

There should probably be one question where people post everything like this, just like the unscramble question. – jimmy23013 – 2015-01-09T15:57:55.110

I think I have some obfuscated code somewhere that I could post if we decide it's a good fit. – KSFT – 2015-01-09T16:06:28.497

@MartinBüttner So...has it been decided not to allow it? Has anyone made a post on meta about it? – KSFT – 2015-01-09T19:47:01.347

@KSFT I don't think anything has been decided, because I haven't seen a meta post on it. – Martin Ender – 2015-01-09T19:56:27.273

Answers

3

This program sums all natural numbers strictly below x (rounded down to the nearest multiple of 10, I don't think this was intended), that are divisible by either 5 or 3. For example: x=30 returns: 0+3+5+6+9+10+12+15+18+20+21+24+25+27 = 195

PS what the heck? Seriously,

for(int i = 0;i < x;i++) S+= (i%3||i%5)?i:0;

would've been easier to write down, to read, and to execute.

Explanation arrives as soon as I get on my actual PC.

Gerwin

Posted 2015-01-09T13:51:01.780

Reputation: 126

If this is right, how the hell did you find it? (some pseudo code please ^^) – dwana – 2015-01-09T14:51:07.747

@Gerwin, nice! You're right about the arbitrary restriction, it was not intended, that's why I mentioned it in the post. To answer your "question", as said earlier, this was one of my first programs (years ago) and I've spent a ridiculous amount of time on this trivial problem. I know that a normal-thinking person in his right mind wouldn't solve it like that. I especially like how the numbers 3 and 5 do not even appear anywhere in the code.

– YouniS Bensalah – 2015-01-09T14:58:32.110

now let's hope he doesn't edit his question do what and HOW ^^ (have an upvote) – dwana – 2015-01-09T15:11:33.463

I wonder if it's related to Project Euler problem 1.

– KSFT – 2015-01-09T15:19:06.993

@KSFT, see the link in my comment? ^^ – YouniS Bensalah – 2015-01-09T15:20:39.023

@younishd Ah, I didn't notice it. sum(i for i in range(1000) if i%3==0 or i%5==0) – KSFT – 2015-01-09T15:28:25.083

@Gerwin, add spoil tags to your answer >! – YouniS Bensalah – 2015-01-09T15:58:48.613