fork(5) download
  1. #!/usr/bin/python
  2.  
  3. import time
  4. import sys
  5.  
  6.  
  7. def output(l):
  8. if isinstance(l,tuple): map(output,l)
  9. else: print l,
  10.  
  11.  
  12. #more boring faster way ------------------------
  13. def diophantine_f(ilist,n):
  14. if n == 0:
  15. output(ilist)
  16. print
  17. else:
  18. for i in xrange(n,0,-1):
  19. diophantine_f((ilist,i), n-i)
  20.  
  21.  
  22. #crazy fully recursive way --------------------
  23. def diophantine(ilist,n,i):
  24. if n == 0:
  25. output(ilist)
  26. print
  27. elif i > 0:
  28. diophantine(ilist, n, diophantine((ilist,i), n-i, n-i))
  29. return 0 if len(ilist) == 0 else ilist[-1]-1
  30.  
  31.  
  32. ##########################
  33. #main
  34. ##########################
  35. try:
  36.  
  37. if len(sys.argv) == 1: x=int(raw_input())
  38. elif len(sys.argv) == 2: x=int(sys.argv[1])
  39. else: raise ValueError
  40.  
  41. if x < 1: raise ValueError
  42.  
  43. print "\n"
  44. #diophantine((),x,x)
  45. diophantine_f((),x)
  46. print "\nelapsed: ", time.clock()
  47.  
  48. except ValueError:
  49. print "usage: ", sys.argv[0], " <Z+>"
  50. exit(1)
  51.  
Success #stdin #stdout 0.02s 4712KB
stdin
6
stdout

6
5 1
4 2
4 1 1
3 3
3 2 1
3 1 2
3 1 1 1
2 4
2 3 1
2 2 2
2 2 1 1
2 1 3
2 1 2 1
2 1 1 2
2 1 1 1 1
1 5
1 4 1
1 3 2
1 3 1 1
1 2 3
1 2 2 1
1 2 1 2
1 2 1 1 1
1 1 4
1 1 3 1
1 1 2 2
1 1 2 1 1
1 1 1 3
1 1 1 2 1
1 1 1 1 2
1 1 1 1 1 1

elapsed:  0.02