Skip to content

Instantly share code, notes, and snippets.

@rw-r-r-0644
Last active November 30, 2020 14:07
Show Gist options
  • Save rw-r-r-0644/024a4823adba2169a8ffc0d60cadb4b7 to your computer and use it in GitHub Desktop.
Save rw-r-r-0644/024a4823adba2169a8ffc0d60cadb4b7 to your computer and use it in GitHub Desktop.
#!/bin/python3
# manhattan.py
# Solves "paths of Manhattan (2d)" problem
#
# Copyright(C) 2020 rw-r-r-0644
# This file is under GNU GPLv2
import math, sys
def tartaglia(n, k):
return math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
def manhattan(i, j):
return tartaglia(i + j, min(i, j))
i = int(sys.argv[1])
j = int(sys.argv[2])
print(manhattan(i, j))
; manhattan.rkt
; Solves "paths of Manhattan (2d)" problem
;
; Copyright(C) 2020 rw-r-r-0644
; This file is under GNU GPLv2
(define factorial
(lambda (n)
(if (= n 0) 1
(* n (factorial (- n 1))))
))
(define tartaglia
(lambda (n k)
(quotient (factorial n) (* (factorial k) (factorial (- n k))))
))
(define manhattan-2d
(lambda (i j)
(tartaglia (+ i j) (min i j))
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment