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

Pages: 1-

BESTEST ALGORITHM

Name: Anonymous 2009-01-17 21:20

For this problem???

Get a list of numbers from user or file.  There may be only 1 number or their could be thousands.

Then determine whether any subset of this set of numbers sums up to 0.

Name: Anonymous 2009-01-17 21:24

Forget it, it's NP-complete.

Name: Anonymous 2009-01-17 21:49

Stop trolling me, FrozenVoid

Name: Anonymous 2009-01-17 22:14

I would use a quantum sort

Name: Anonymous 2009-01-17 22:21

A*

Name: Anonymous 2009-01-17 22:47

USE DYNAMIC PROGRAMMING

Name: Anonymous 2009-01-18 1:46

try using a bubblesort!

Name: Anonymous 2009-01-18 3:58

I propose that you use PCNN¹.

________________

¹ Pulse-Coupled Neural Networks (PCNNs) are neural models proposed by modeling a cat’s visual cortex and developed for high-performance biomimetic image processing. (Wikipedia et al., 2008)

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !FrOzEn2BUo 2009-01-18 4:51

>>8
Image processing isn't designed for calculating number sets.

_________________________
orbis terrarum delenda est

Name: Anonymous 2009-01-18 5:10

Name: Anonymous 2009-01-18 5:14

Why all the hate on web developers? Can't we all just get along

Name: Anonymous 2009-01-18 7:57

LIKE TO PARTY

Name: Anonymous 2009-01-18 9:58

import Data.Array

subsetsum xs = foldl f (listArray (min, max) (repeat False)) xs ! 0
  where f s x = listArray (min, max) (map g [min .. max])
          where g i = i == x || s ! i || inRange (min, max) (i - x) && s ! (i - x)
        min = sum (filter (< 0) xs)
        max = sum (filter (> 0) xs)

Name: Anonymous 2009-01-18 10:27

import List

hasZeroSumSubset = any (any ((0 =) . sum) . inits) . permutations

Name: >>14 2009-01-18 10:32

Forgot my [code].

import List

hasZeroSumSubset = any (any ((0 =) . sum) . inits) . permutations

Name: Anonymous 2009-01-18 13:25

ok now in a real language (ie. C)

Name: Anonymous 2009-01-18 13:37

>>16
$ cat > subsetsum.hs
module SubsetSum where

import Data.Array

subsetsum xs = foldl f (listArray (min, max) (repeat False)) xs ! 0
  where f s x = listArray (min, max) (map g [min .. max])
          where g i = i == x || s ! i || inRange (min, max) (i - x) && s ! (i - x)
        min = sum (filter (< 0) xs)
        max = sum (filter (> 0) xs)
$ ghc -C -c subsetsum.hs

Name: Anonymous 2009-01-18 15:20

bool subsetsum(int *xs, int n) {
    int neg = 0, pos = 0;
    for (int i = 0; i < n; i++) {
        if (xs[i] < 0) {
            neg += xs[i];
        } else {
            pos += xs[i];
        }
    }

    int *p = calloc(pos - neg + 1, sizeof(*p));
    int *q = calloc(pos - neg + 1, sizeof(*q));
    for (int i = 0; i < n; i++) {
        for (int j = neg; j <= pos; j++) {
            q[j - neg] = j == xs[i]
                || p[j - neg]
                || neg <= j - xs[i] && j - xs[i] <= pos && p[j - xs[i] - neg];
        }
        int *t = p; p = q; q = t;
    }

    bool ret = p[-neg];
    free(p);
    free(q);
    return ret;
}

Name: Anonymous 2009-01-18 21:05


' freebasic
' first command line argument is file to open
' returns exit status 0 if there is, 1 if there is not

dim a(0) as long uinteger : dim na as integer=0 ' array of read values, number of values in array
dim ia as integer=0, nt as integer=0 ' ia is recently read value from file, nt is subset total
dim l1 as integer=0, l2 as integer=0 ' loop vars
dim f as integer=0 ' set to -1 if subset = 0

open command$(1) for input as 1
do until eof(1)
input#1, ia
na=na+1
redim preserve a(na)
a(na)=ia

for l2=1 to na
nt=0
for l1=l2 to na
nt=nt+a(l1)
next
if nt=0 then f=-1:exit do
next
if nt=0 then f=-1:exit do
loop

if f then exit 0 else exit 1
close 1

Name: Anonymous 2009-01-19 17:02

I solved this a few years ago with an O(n) algorithm written in the programming language HTML.

Name: Anonymous 2009-01-19 18:05

I solved this a few years ago with an O(log n) algorithm written in the programming language BBCode.

Name: Anonymous 2009-01-19 18:28

>>21
Haha, yeah man that sucks. Come check out my pics at
http://www.flickr.com/photos/22931539@N05/

Name: Anonymous 2009-01-21 1:36

>>18
does this work?

Name: Anonymous 2009-01-22 17:16

>>23
compile and test it out

Name: Trollbot9000 2009-07-01 10:44


image board in bash.

Name: Anonymous 2010-12-10 13:20

Name: Anonymous 2010-12-17 1:30

Xarn is a bad boyfriend

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