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

Challenge: fastest program wins

Name: Anonymous 2009-04-14 12:59

Write a program in your language that
1.opens a file as arbitrary precision integer.
2.divides the number by 3.
3.stores the binary result in another file.

Name: Anonymous 2009-04-14 13:02

int x = file.open(someFile)/3;
x.binaryWrite(anotherFile);

Name: Anonymous 2009-04-14 13:02

Define the format of the integer and its limits.
Without limiting the language, the fastest will be in hand-optimized asm. If you take a high level language into consideration, then, the result varies widely depending on the compiler and the design choices of your language.

Due to these inconsistencies, I conclude this is a troll thread.

Name: Anonymous 2009-04-14 13:17

Do your own homework

Name: Anonymous 2009-04-14 13:24

main = liftM (show . (/ 3) . read) (readFile "in") >>= writeFile "out"

Name: Anonymous 2009-04-14 13:26

In before best Java scalable enterprise solutions, pseudo-functional FIOC, Haskell one-liner that uses extensive amounts of $ that are really parens, and a C program that actually works.

Name: Anonymous 2009-04-14 13:30

Your program is fast, but does it scale?

Name: Anonymous 2009-04-14 13:53


#define FUNCTION_RETURN_TYPE void
#define FUNCTION_NAME writeLoop
#define FUNCTION_PARAMETERS void
#define START_FUNCTION {
#define END_FUNCTION }
#define OPENING_DEVICE file
#define OPENING_FUNCTION open
#define WRITING_FUNCTION binaryWrite
#define FILE_OPEN_NAME someFile
#define FILE_WRITE_NAME anotherFile
#define MATH_OPERATION /
#define MATH_OPERATION_VALUE 3
#define NUMBER_OF_FILES 1
#define LOOP_INITIAL 0
#define INCREMENT += 1;
#define FOR_LOOP_BEGIN for
#define FOR_INITIALIZE_BEGIN (
#define FOR_INITIALIZE_END ;
#define FOR_CONDITION_BEGIN
#define FOR_CONDITION_END ;
#define FOR_INCREMENT_BEGIN
#define FOR_INCREMENT_END ) {
#define FOR_LOOP_END }
#define PARAMETERS_BEGIN (
#define PARAMETERS_END )
#define END_STATEMENT ;
#define INDEX_BEGIN [
#define INDEX_END ]
#define HEADERS #include <stdio.h> \
                #include <sys/stat.h> \
                #include <unistd.h> \
                #include <fcntl.h> \
                #include <stdlib.h>

HEADERS

typedef int arbitrary_precision_integer_t;

FUNCTION_RETURN_TYPE
FUNCTION_NAME
PARAMETERS_BEGIN
   FUNCTION_PARAMETERS
PARAMETERS_END
START_FUNCTION

int loopCount;
arbitrary_precision_integer_t arbitraryPrecisionIntegerFiles[NUMBER_OF_FILES];

FOR_LOOP_BEGIN
   FOR_INITIALIZE_BEGIN
      loopCount = LOOP_INITIAL;
   FOR_INITIALIZE_END
   FOR_CONDITION_BEGIN
      loopCount < NUMBER_OF_FILES
   FOR_CONDITION_END
   FOR_INCREMENT_BEGIN
      loopCount INCREMENT;
   FOR_INCREMENT_END
   loopCount = OPENING_DEVICE.OPENING_FUNCTION
               PARAMETERS_BEGIN
                  FILE_READ_NAME
                  O_RDONLY
               PARAMETERS_END
               END_STATEMENT
   OUTPUT_FILE = OPEN_DEVICE.OPENING_FUNCTION
                 PARAMETERS_BEGIN
                    FILE_WRITE_NAME,
                    O_WRONLY
                 PARAMETERS_END
                 END_STATEMENT
   OUTPUT_FILE.BINARY_WRITE
             PARAMETERS_BEGIN
                arbitraryPrecisionIntegerFiles
                   INDEX_BEGIN
                      loopCount
                   INDEX_END
             PARAMETERS_END
             END_STATEMENT
FOR_LOOP_END
END_FUNCTION


SCALABLE C

I will send it to the next department so they can implement ERROR CHECKING!

Name: Anonymous 2009-04-14 13:56

>>6
Haskell one-liner that uses extensive amounts of $ that are really parens
In after, you mean.

Additionally, $ is the operator for low-precedence function application -- it's has the same effect as using parens to change evaluation order in most situations, but can be used for much, much more.

Name: Anonymous 2009-04-14 14:03

>>8
Is it wrong that I came buckets to that program?

Name: Anonymous 2009-04-14 14:06

#define FUNCTION_RETURN_TYPE void
#define FUNCTION_NAME writeLoop
#define FUNCTION_PARAMETERS void
#define START_FUNCTION {
#define END_FUNCTION }
#define OPENING_DEVICE file
#define OPENING_FUNCTION open
#define WRITING_FUNCTION write
#define CLOSING_FUNCTION close
#define FILE_READ_NAME someFile
#define FILE_WRITE_NAME anotherFile
#define MATH_OPERATION /
#define MATH_OPERATION_VALUE 3
#define NUMBER_OF_FILES 1
#define LOOP_INITIAL 0
#define INCREMENT += 1;
#define FOR_LOOP_BEGIN for
#define FOR_INITIALIZE_BEGIN (
#define FOR_INITIALIZE_END ;
#define FOR_CONDITION_BEGIN
#define FOR_CONDITION_END ;
#define FOR_INCREMENT_BEGIN
#define FOR_INCREMENT_END ) {
#define FOR_LOOP_END }
#define PARAMETERS_BEGIN (
#define PARAMETERS_END )
#define END_STATEMENT ;
#define INDEX_BEGIN [
#define INDEX_END ]
#define HEADERS #include <stdio.h> \
                #include <sys/stat.h> \
                #include <unistd.h> \
                #include <fcntl.h> \
                #include <stdlib.h>

HEADERS

typedef int arbitrary_precision_integer_t;

FUNCTION_RETURN_TYPE
FUNCTION_NAME
PARAMETERS_BEGIN
   FUNCTION_PARAMETERS
PARAMETERS_END
START_FUNCTION

int loopCount, readFile, writeFile;
arbitrary_precision_integer_t arbitraryPrecisionIntegerFiles[NUMBER_OF_FILES];

FOR_LOOP_BEGIN
   FOR_INITIALIZE_BEGIN
      loopCount = LOOP_INITIAL;
   FOR_INITIALIZE_END
   FOR_CONDITION_BEGIN
      loopCount < NUMBER_OF_FILES
   FOR_CONDITION_END
   FOR_INCREMENT_BEGIN
      loopCount INCREMENT;
   FOR_INCREMENT_END
   readFile = OPENING_DEVICE.OPENING_FUNCTION
               PARAMETERS_BEGIN
                  FILE_READ_NAME,
                  O_RDONLY
               PARAMETERS_END
               END_STATEMENT
   writeFile = OPEN_DEVICE.OPENING_FUNCTION
                 PARAMETERS_BEGIN
                    FILE_WRITE_NAME,
                    O_WRONLY | O_APPEND
                 PARAMETERS_END
                 END_STATEMENT
   WRITE
      PARAMETERS_BEGIN
         writeFile,
         readFile MATH_OPERATION MATH_OPERATION_VALUE,
         sizeof(int)
      PARAMETERS_END
      END_STATEMENT
   CLOSING_FUNCTION
      PARAMETERS_BEGIN
         writeFile
      PARAMETERS_END
   CLOSING_FUNCTION
      PARAMETERS_BEGIN
         readFile
      PARAMETERS_END
FOR_LOOP_END
END_FUNCTION


There, that's closer to something to work with.  I think I started writing SEPPLES on the last one.

Name: Anonymous 2009-04-14 14:10

in b4 JAVA

Name: Anonymous 2009-04-14 14:20

>>11
I think if people saw that, there would be no more need for Java.  You still have two sections with C++, but you just have to remove the "OPENING_DEVICE" and "OPENING_DEVICE."

Name: Anonymous 2009-04-14 16:51

>>8
BRINGING C TO THE ENTERPRISE

Name: Anonymous 2009-04-14 17:13

I've started running my Ruby implementation. I'll give you the benchmark stats once it's done.

Name: Anonymous 2009-04-14 17:44

from sys import argv
open(argv[2], 'w').write(bin(int(open(argv[1], 'r').read()) // 3))


The challenge is badly specified, uninteresting and useless. Bring back Practical /prog/ Challenges!

Name: Anonymous 2009-04-14 17:55

Bring back Practical /prog/ Challenges!
In that case, implement a POSIX-compliant implementation of ls

Name: Anonymous 2009-04-14 18:04

>>17
Anonix anyone?

Name: Anonymous 2009-04-14 22:54

>>8
that's beautiful.

Name: Anonymous 2009-04-15 0:20

It's fast, believe me.

import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;
import java.util.Scanner;

public class Lol {
  private static final String IN  = "file1.txt";
  private static final String OUT = "file2.bin";

  public static void main(String[] args) {
    try {
      Scanner scanner = new Scanner(new File(IN));
      BigInteger n = scanner.nextBigInteger();
      scanner.close();
 
      n.divide(BigInteger.valueOf(3));
      byte[] b = n.toByteArray();

      FileOutputStream out = new FileOutputStream(new File(OUT));
      out.write(b);
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Name: Anonymous 2009-04-15 0:54

>>20
Doesn't meet the spec, uses BigInteger and default I/O buffering. 0/10.

Name: Anonymous 2009-04-15 0:58

>>21
BigInteger is arbitrary precision, and the default I/O buffering is sufficient

Try it aganist your "C" programs and see how it stacks up, I dare you

Name: Anonymous 2009-04-15 1:36

>>22
try this one.
#include <stdio.h>
#include <gmp.h>

int main(int argc, char **argv)
{ if(argc > 2)
  { FILE *f;
    mpz_t n;
    mpz_init(n);
    if(f = fopen(argv[1], "r"))
    { mpz_inp_str(n, f, 10);
      fclose(f);
      mpz_tdiv_q_ui(n, n, 3);
      if(f = fopen(argv[2], "w"))
      { mpz_out_str(f, 10, n);
        fputs("\n", f);
        fclose(f); }
      mpz_clear(n); }}
  return 0; }

Name: Anonymous 2009-04-15 1:50

When the fire leaps amidst the northern maelstrom I will rise covered in glory with all the underaged valkyries chewing my cum

Name: Anonymous 2009-04-15 1:51

>>22
BigInteger has obnoxious overhead for such a trvial problem and the default I/O buffering is NOT sufficient given it will be the bottleneck in this problem.

Name: Anonymous 2009-04-15 1:54

>>23
Holy fuck, take that indentation somewhere else. Jesus fucking christ it burns my eyes.

Name: Anonymous 2009-04-15 2:28

>>26
it doesn't matter what it looks like. it's faster than yours.

Name: Anonymous 2009-04-15 2:38

>>27
it doesn't matter what it looks like.
Yes, it does.

Also, your program doesn't do >>1.3, however you interpret it.

Name: Anonymous 2009-04-15 2:41

>>28
are you saying that the resulting file isn't made up of bits? what the fuck is it then?

Name: Anonymous 2009-04-15 3:09

>>29
I'm asking for the output to be NUMBERS.
As in: 2938175 or whatever, but expressed in BINARY as NUMBERS

Name: Anonymous 2009-04-15 4:05

>>30
OP just asked for a binary result.
as long as the output is a bunch of bits, it's correct.

Name: Anonymous 2009-04-15 4:56

>>32
But is it in NUMBERS?

Name: Anonymous 2009-04-15 5:27

>>32
Depends how you interpret binary zeros and ones. As symbols for truth function results or as numbers in their own right.

Name: Anonymous 2009-04-15 6:22

>>30,32
Do you mean ASCII CHARACTERS?  Because there's a huge fucking difference between a bloated mass of ASCII CHARACTERS and the actual, numerical, binary result.  Say what you mean (or stop meaning such a retarded thing in the first place), or you have no right to be a dick when you don't get it.

Name: Anonymous 2009-04-15 10:44

This is best implemented using long division and not reading the entire file in at once. Thus it will scale nicely.

Here's my C program to do this:

/* div3.c */

#include <errno.h>
#include <stdio.h>

typedef unsigned char byte;

int main(int argc, char *argv[])
{
    FILE *fin;
    FILE *fout;
    byte remainder = 0;
    byte remdiv = 0;
    byte buf[4096];
    size_t bytes_read, nums_cnt, n;
   
    if (argc < 3)
    {
        fprintf(stderr, "usage: div3 infile outfile\n");
        return 1;
    }
   
    if (!(fin = fopen(argv[1], "rb")))
    {
        fprintf(stderr, "can't open input file due to error %u\n", errno);
        return 2;
    }
   
    if (!(fout = fopen(argv[2], "wb")))
    {
        fprintf(stderr, "can't open output file due to error %u\n", errno);
        return 3;
    }

    while(!feof(fin))
    {
        bytes_read = fread(&buf, 1, sizeof(buf), fin);
        if (ferror(fin))
        {
            fprintf(stderr, "error %u while reading file\n", errno);
            return 4;
        }

        for (n=0; n<bytes_read; n++)
        {
            switch(remainder)
            {
            case 0:
                remdiv = 0;
                break;
            case 1:
                remdiv = 85;
                break;
            case 2:
                remdiv = 170;
            }
            remainder += buf[n] % 3;
            buf[n] = buf[n]/3 + remainder/3 + remdiv;
            remainder %= 3;
        }
       
        fwrite(buf, 1, bytes_read, fout);
        if (ferror(fout))
        {
            fprintf(stderr, "error %u while writing file\n", errno);
            return 5;
        }
    }
   
    fclose(fin);
    fclose(fout);
   
    return 0;
}

Name: Anonymous 2009-04-15 10:52

>>24
Thank you for your contribution.

Name: Anonymous 2009-04-16 7:13

>>35
Now make it work using the machine's word size instead of by bytes.

Name: Anonymous 2009-04-16 10:47

linux is so fucking gay. imma gonna buy a second hand mac or something.

Name: Anonymous 2009-04-16 10:48

>>38
$2500, please.

Name: Anonymous 2009-04-16 10:56

MAC OS is like BSD, only apples charge for it. Why would I pay for something that I get for free, and without pig disgusting extras like web 2.0 GUI or screenlets?

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