Print all multiples of 32 from 0 to `n`...imperfectly

3

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!

Isiah Meadows

Posted 2014-08-15T12:38:46.280

Reputation: 1 546

Question was closed 2016-04-24T06:14:06.350

Does printing nothing work? I have an idea, but I'm not sure if that would omit more numbers than the maximum. – KSFT – 2015-01-16T16:34:12.890

@KSFT Actually, since it is a popularity contest, I'm going to modify that rule. – Isiah Meadows – 2015-01-20T14:50:25.767

Why do you prohibit certain characters in the program source? – FUZxxl – 2015-02-04T09:19:51.007

I'll clarify it as ASCII. It's poorly worded – Isiah Meadows – 2015-02-04T09:20:49.763

4

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.

– James – 2016-04-23T20:22:35.650

"it must match the following regex" -- is multiline mode on? – John Dvorak – 2014-08-15T12:41:25.317

@JanDvorak It matches the whole, not per line. I forget which is multiline. – Isiah Meadows – 2014-08-15T12:41:49.290

floored division is normally denoted by div. – John Dvorak – 2014-08-15T12:43:55.553

1Is 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. – Cameron – 2014-08-16T16:23:32.687

@Cameron Fixed. – Isiah Meadows – 2014-08-17T06:40:16.290

Well the title still says 32 while in the objective it is state as 42, so what now? – flawr – 2014-08-17T07:52:07.040

6I really shouldn't program. – Soham Chowdhury – 2014-09-05T02:52:07.080

Answers

1

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);
        }
    }
}

Keith Randall

Posted 2014-08-15T12:38:46.280

Reputation: 19 865

@Jakube: It does give 2 wrong results for n=63. It doesn't print 0 and 32 when it should. – Keith Randall – 2015-02-04T15:31:23.570

0

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)) {

NobodyImportant

Posted 2014-08-15T12:38:46.280

Reputation: 9

1Underhanded: rule 6 is important. The code should look like it should work. – Isiah Meadows – 2015-02-04T08:52:27.370