Last active
December 20, 2015 03:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
| ?- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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