Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Fun Programming Contest - Win $10

Name: Anonymous 2007-09-24 4:44 ID:qljv4pIe

I have a fun little programming contest for y'all! You are writing a function named foo() that takes in five arguments and outputs a 32-bit integer. The user who writes the most efficient solution meeting all of the specifications outlined below will receive a $10 cash prize. (We have thousands of test cases to spread out the results.)

function foo(a, b, c, d, e) {
// your code goes here
return out;
}

Inputs:
u8 a; // range: 0-24
bool b;
u8 c; // range: 0-255
bool d;
u32 e; // range 0-2^32-1

Output:
u32 out; // range 0-2^32-1

Here are the four restrictions for the output:
1. (out mod 25) must be equal to a
2. if b = FALSE, then out mod 255 must be between 0 and c-1, inclusive
3. if b = TRUE, then out mod 255 must be between c and 255, inclusive
4. if d = TRUE, then ((out XOR e) / 65536) XOR (out XOR e) % 65536)) must be < 8

Example:
foo(1, 0, 31, 1, 0) returns 1, which satisfies all of the conditions above.
foo(3, 1, 31, 1, 3162282670) returns 4063024378
There may be several possible values for out, but a value that satisfies all four conditions above must be returned.

You may use Java, C, or C++.

All solutions should be sent to markus.yeh (at) gmail.com . Only your most recent submission will be entered into the contest. The entry deadline is Wednesday, September 26 at 12:00 a.m. Pacific Time, and winners will be announced before Saturday, September 29 at 12:00 a.m. Pacific Time.

Good luck!

Name: Anonymous 2007-09-24 22:50 ID:Heaven

Ignore any stupid mistakes

$ cat stupid.c; gcc -Wall -O2 stupid.c -o stupid && time ./stupid
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>


uint32_t foo( uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint32_t e )
{
    uint32_t out = 0;
    uint32_t tmp;
    uint32_t max;

    max = 0xFFFFFFFFuL - a;

    for( out = a; out < max; out += 25 )
    {
        if ( d )
        {
            tmp = out ^ e;

            if ( (((tmp / 65536) ^ tmp) % 65536) >= 8 )
            {
                continue;
            }
        }

        if ( b )
        {
            tmp = out % 255;

            if ( tmp < c )
            {
                continue;
            }
        }
        else
        {
            if ( out % 255 > c-1 )
            {
                continue;
            }
        }

        return out;
    }

    return 0;
}


int main( int argc, char ** argv )
{
    printf( "r=%u\n", foo( 1, 0, 31, 1, 0 ) );
    printf( "r=%u\n", foo( 3, 1, 31, 1, 3162282670uL ) );
    printf( "r=%u\n", foo( 3, 1, 31, 1, 0xFFFFFFFFuL ) );
    return 0;
}


r=1
r=75478
r=1114128

real    0m0.005s
user    0m0.000s
sys     0m0.000s

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List