Skip to content

Instantly share code, notes, and snippets.

@Koronen
Last active December 20, 2015 03:19
Show Gist options
  • Save Koronen/6063134 to your computer and use it in GitHub Desktop.
Save Koronen/6063134 to your computer and use it in GitHub Desktop.
Programming puzzle: Write a function which given a set of integers determines if there exists a subset such that the sum of its elements is zero.
koronen@paridae$ prolog
GNU Prolog 1.3.0
By Daniel Diaz
Copyright (C) 1999-2007 Daniel Diaz
| ?- consult('puzzle.pl').
compiling /home/koronen/puzzle.pl for byte code...
/home/koronen/puzzle.pl compiled, 5 lines read - 608 bytes written, 11 ms
yes
| ?- puzzle([]).
no
| ?- puzzle([1,2,3]).
no
| ?- puzzle([1,2,3,-2]).
true ? ;
no
| ?-
puzzle(InputList) :-
sublist(OutputList, InputList),
length(OutputList, OutputListLength),
OutputListLength >= 2,
sum_list(OutputList, 0),
!.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment