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

Pages: 1-

ENTERPRISE LEVEL FIBS

Name: Anonymous 2009-12-04 20:00

how does Prague like my ENTERPRISE LEVEL fibs() implementation?
[size]
#include <stdlib.h>
#include <stdio.h>
#define MATSIZE(n) n * n * sizeof(int)
void mprint(int *mat, int size) {
  int i, j;
  for(i = 0; i <size; ++i) {
    for(j=0; j <size; ++j)
      printf("%d ", mat[i * size + j]);
    printf("\n");
  }
}
int dotp(int *mat1, int row, int *mat2, int col, int size, int mod) {
  int ind, prod = 0;
  for(ind = 0;ind < size; ind++)
    prod = (prod + mat1[row * size + ind] * mat2[ind * size + col]) % mod;
  return prod;
}
int *mmul(int *matrix1, int *matrix2, int size, int mod) {
  int row, col, *product = malloc(MATSIZE(size));
  for(row = 0; row < size; row++)
    for(col = 0; col < size; col++)
      product[row * size + col] = dotp(matrix1, row, matrix2, col, size, mod);
  return product;
}
/*runs in constant time w.r.t. pow*/
int *lpow(int *matrix, int size, int pow, int mod) {
  int *val[32], *result, *old, ind, i, j;
  val[0] = matrix;
  for(ind = 1; ind < 32; ind++)
    val[ind] = mmul(val[ind-1], val[ind-1], size, mod);
  result = malloc(MATSIZE(size));
  for(i=0;i<size;i++) for(j=0;j<size;j++) result[i * size + j] = (i == j);
  for(ind = 0; ind < 32; ind++)
    if(pow & 1<<ind) {
      old = result;
      result = mmul(result, val[ind], size, mod);
      free(old);
      free(val[ind]);
    }
    else
      free(val[ind]);
  return result;
}
/*calculates fibs(n) modulo mod*/
int fibs(int n, int mod) {
  int *mat1, *prod, ret;
  mat1 = malloc(MATSIZE(2));
  mat1[0] = mat1[1] = mat1[2] = 1;
  mat1[3] = 0;
  prod = lpow(mat1, 2, n, mod);
  ret = prod[1];
  free(prod);
  return ret;
}
int main(int anusc, char *anusv[]) {
  printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n",
        fibs(10, 16384),
        fibs(100, 16384),
        fibs(1000, 16384),
        fibs(10000, 16384),
        fibs(100000, 16384),
        fibs(1000000, 16384),
        fibs(10000000, 16384),
        fibs(100000000, 16384)); /*modulus can be increased on 64-bit platforms*/
  return 0;
}[/code]

Name: Anonymous 2009-12-04 20:01

Whoops, fucked the first [code] tag up.
how does Prague like my ENTERPRISE LEVEL fibs() implementation?

#include <stdlib.h>
#include <stdio.h>
#define MATSIZE(n) n * n * sizeof(int)
void mprint(int *mat, int size) {
  int i, j;
  for(i = 0; i <size; ++i) {
    for(j=0; j <size; ++j)
      printf("%d ", mat[i * size + j]);
    printf("\n");
  }
}
int dotp(int *mat1, int row, int *mat2, int col, int size, int mod) {
  int ind, prod = 0;
  for(ind = 0;ind < size; ind++)
    prod = (prod + mat1[row * size + ind] * mat2[ind * size + col]) % mod;
  return prod;
}
int *mmul(int *matrix1, int *matrix2, int size, int mod) {
  int row, col, *product = malloc(MATSIZE(size));
  for(row = 0; row < size; row++)
    for(col = 0; col < size; col++)
      product[row * size + col] = dotp(matrix1, row, matrix2, col, size, mod);
  return product;
}
/*runs in constant time w.r.t. pow*/
int *lpow(int *matrix, int size, int pow, int mod) {
  int *val[32], *result, *old, ind, i, j;
  val[0] = matrix;
  for(ind = 1; ind < 32; ind++)
    val[ind] = mmul(val[ind-1], val[ind-1], size, mod);
  result = malloc(MATSIZE(size));
  for(i=0;i<size;i++) for(j=0;j<size;j++) result[i * size + j] = (i == j);
  for(ind = 0; ind < 32; ind++)
    if(pow & 1<<ind) {
      old = result;
      result = mmul(result, val[ind], size, mod);
      free(old);
      free(val[ind]);
    }
    else
      free(val[ind]);
  return result;
}
/*calculates fibs(n) modulo mod*/
int fibs(int n, int mod) {
  int *mat1, *prod, ret;
  mat1 = malloc(MATSIZE(2));
  mat1[0] = mat1[1] = mat1[2] = 1;
  mat1[3] = 0;
  prod = lpow(mat1, 2, n, mod);
  ret = prod[1];
  free(prod);
  return ret;
}
int main(int anusc, char *anusv[]) {
  printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n",
        fibs(10, 16384),
        fibs(100, 16384),
        fibs(1000, 16384),
        fibs(10000, 16384),
        fibs(100000, 16384),
        fibs(1000000, 16384),
        fibs(10000000, 16384),
        fibs(100000000, 16384)); /*modulus can be increased on 64-bit platforms*/
  return 0;
}

Name: Anonymous 2009-12-04 20:13

>>2
Enterprise applications are written in Java, not Cee. Now GTFO.

Name: Anonymous 2009-12-04 22:27

>>3
u mena liek oracledb

Name: Anonymous 2009-12-06 15:49

I'll see your ENTERPRISE and raise you a SCALEABLE BUSINESS SOLUTION fizzbuzz


#include <iostream>

class PolicyBase {
public:
    void Process(int i) {
        if(!DoProcess(i) && next)
            next->Process(i);
    }
    PolicyBase* NextPolicy(PolicyBase* p) {
        next = p;
        return next;
    }
    PolicyBase():next(0) {}

private:
    virtual bool DoProcess(int i) { return false; };
    PolicyBase* next;
};

class NumberPolicy:public PolicyBase {
private:
    bool DoProcess(int i) {
        std::cout << i << std::endl;
        return true;
    }
};


class FizzBuzzPolicy:public PolicyBase {
private:
    bool DoProcess(int i) {
        if((i % 3) || (i % 5)) return false;

        std::cout << "FizzBuzz" << std::endl;
        return true;
    }
};

class FizzPolicy:public PolicyBase {
private:
    bool DoProcess(int i) {
        if((i % 3)) return false;

        std::cout << "Fizz" << std::endl;
        return true;
    }
};

class BuzzPolicy:public PolicyBase {
private:
    bool DoProcess(int i) {
        if((i % 5)) return false;

        std::cout << "Buzz" << std::endl;
        return true;
    }
};

int main() {
    PolicyBase b;
    b.NextPolicy(new FizzBuzzPolicy)->NextPolicy(new FizzPolicy)->NextPolicy(new BuzzPolicy)->NextPolicy(new NumberPolicy);
    for(int i=1;i<=100;++i) {
        b.Process(i);
    }

    return 0;
}

Name: Anonymous 2009-12-06 15:58

>>5
A scalable business solution would at least deallocate those policies, preferably with an AllocationPolicy (perhaps it would be a template parameter).

Name: Anonymous 2009-12-07 0:43

>>5
Like all good SCALABLE ENTERBUSINESS SOLUTIONPRISES, it doesn't even solve the original problem

Name: Anonymous 2009-12-07 1:14

>>2
And again we see how LISP is so much more elegant and terse.

(defun mat-mul (a b)
  "Matrix multiplication"
  (let ((files_a (car (array-dimensions a)))
        (files_b (car (array-dimensions b)))
        (columns_a (cadr (array-dimensions a)))
        (columns_b (cadr (array-dimensions b)))
        (result nil))

        (if (eq files_a nil) (setq files_a 0))
        (if (eq files_b nil) (setq files_b 0))
        (if (eq columns_a nil) (setq columns_a 0))
        (if (eq columns_b nil) (setq columns_b 0))

        (unless (= columns_a files_b) (return-from mat-mul nil))
        (let ((aux nil))
          (push columns_b aux)
          (push files_a aux)
          (setq result (make-array aux)))

        (do ((i 0 (+ i 1)))
          ((= i files_a) t)
          (do ((j 0 (+ j 1)))
            ((= j columns_b) t)
            (let ((sum 0))
              (do ((k 0 (+ k 1)))
                ((= k columns_a) t)
                (incf sum (* (aref a i k) (aref b k j))))
              (setf (aref result i j) sum))))
        result))

(defun divmod (a b)
  (values (floor (/ a b)) (mod a b)))

(defun mat-exp (x val)
  "Matrix exponentiation"
    (let ((z x)
          (n (- val 1)))
      (loop while (not (= n 0)) do
            (let ((ndiv 0)
                  (nmod 0))
              (multiple-value-setq (ndiv nmod) (divmod n 2))
              (loop while (= nmod 0) do
                    (setq x (mat-mul x x))
                    (setq n ndiv)
                    (multiple-value-setq (ndiv nmod) (divmod n 2)))
              (setq z (mat-mul z x))
              (decf n 1)))
      z))

(defun fib (n)
  (row-major-aref (mat-exp (make-array '(2 2) :initial-contents
                                       '((1 1) (1 0))) n) 1))


(time (loop for num in (cdr sb-ext:*posix-argv*) do
            (let ((i (parse-integer num :junk-allowed t)))
              (format t "fib(~d) = ~d~%" i (fib i)))))

Name: Anonymous 2011-01-31 20:47

<-- check em dubz

Name: Anonymous 2011-02-03 7:45

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