3
\$\begingroup\$

Objective: Print all multiples of 32 from 0 to an arbitrary number n.

Rules:

  1. One argument will be given: a single integer from 0 to 231 - 1. This will be n here on out.

  2. The first iteration must be 0, and the last iteration must be n. 0 is to be counted as a multiple of 32.

  3. Each time a multiple is encountered, the multiple itself must be printed with no leading whitespace, and then it must be immediately followed by a newline (CR, CRLF, and LF are all acceptable as long as they are consistent.

  4. A bug must be introduced such that a minimum of n+1 floor 32 cases are incorrectly printed/omitted (in the average case).

    • Perfection is allowed for n less than or equal to 32.
  5. Your source code must be in pure ASCII. Exceptions only include APL and others that use non-ASCII operators.

  6. This is an contest. The best code is clear, concise, obvious, and wrong.

Example in C++ (except for clear bug):

// This conforms to the rules, but is a bad entry.
#include <iostream>
#include <cstdlib>
#include <cstdint>

int main(int argc, char* argv[]) {
  int32_t max = (int32_t) atol(argv[1]);
  int32_t i = 0;
  while (i != max) {
    if (!(i % 32) && !((int) (4 * random()) / 4)) {
      cout << i << endl;
    }
    i++;
  }
  return 0;
}

Most popular answer wins, so make it good!

\$\endgroup\$

closed as off-topic by DJMcMayhem, Blue, cat, GamrCorps, Mego Apr 24 '16 at 6:14

  • This question does not appear to be about programming puzzles or code golf within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ "it must match the following regex" -- is multiline mode on? \$\endgroup\$ – John Dvorak Aug 15 '14 at 12:41
  • \$\begingroup\$ @JanDvorak It matches the whole, not per line. I forget which is multiline. \$\endgroup\$ – Isiah Meadows Aug 15 '14 at 12:41
  • 1
    \$\begingroup\$ Is the program supposed to find multiples of 32 or 42? The title says 42, but you said 0 is to be counted as a multiple of 32. \$\endgroup\$ – Cameron Aug 16 '14 at 16:23
  • 6
    \$\begingroup\$ I really shouldn't program. \$\endgroup\$ – Soham Chowdhury Sep 5 '14 at 2:52
  • 4
    \$\begingroup\$ I'm voting to close this question as off-topic because it's an [underhanded] challenge, which was on-topic a year ago, but is now off-topic by community consensus. \$\endgroup\$ – DJMcMayhem Apr 23 '16 at 20:22
1
\$\begingroup\$

C

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
    int n = atoi(argv[1]);
    for(int i = 0; i < n; i++) {
        if(i & 31 == 0) {
            printf("%d\n", i);
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ @Jakube: It does give 2 wrong results for n=63. It doesn't print 0 and 32 when it should. \$\endgroup\$ – Keith Randall Feb 4 '15 at 15:31
0
\$\begingroup\$

modified from the example.

#include <iostream>
#include <cstdlib>
#include <cstdint>

using namespace std;

int main(int argc, char* argv[]) {
  int32_t n = (int32_t) atol(argv[1]);
  int32_t i = 0;

  while (i != n) {
    if (!(i % 32) && n > 32 ? rand() % 4 != 0 : 1) {
      cout << i << endl;
    }
    ++i;
  }
  return 0;
}

Issues:

rule 1: 1 argument. check.
rule 2: first iteration must be 0.
            check.
        last iteration must be n.
            fail. "while (i != n)" means the last iteration is n - 1.
rule 3: print multiples of 32.
            check, except for the bug below
rule 4: print "n+1 floor 32" entries incorrectly.
            not sure what this means, but I certainly don't meet it. I chose to have each multiple of 32 have a 1 in 4 chance of being incorrectly omitted.
rule 5: allowable characters
            I didn't actually check, but I didn't intentionally break this rule.
rule #6: there is no rule #6
rule 6: underhanded
            the program actually prints all numbers from 1 to n - 1, except a random number of multiples of 32.
            the bug is in the operator precedence of the conditional operator:
                if (!(i % 32) && n > 32 ? ((rand() % 4) != 0) : 1) {
            the condition says "if i is a multiple of 32 AND n is greater than 32, (return true if a random number is a multiple of 4. return false if it is not). Otherwise, return true.
            all multiples of 32 have a 1 in 4 chanse of being omitted.
            all other numbers are always printed.
            the line should be:
                if (!(i % 32) && (n > 32 ? rand() % 4 != 0 : 1)) {
\$\endgroup\$
  • 1
    \$\begingroup\$ Underhanded: rule 6 is important. The code should look like it should work. \$\endgroup\$ – Isiah Meadows Feb 4 '15 at 8:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.