Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Created May 13, 2017 03:30
Show Gist options
  • Save friedbrice/7f6be0f9ad1a2a2340ac8042cdf62fdb to your computer and use it in GitHub Desktop.
Save friedbrice/7f6be0f9ad1a2a2340ac8042cdf62fdb to your computer and use it in GitHub Desktop.
triangle
import System.Environment (getArgs)
main = getArgs >>= (putStrLn . triangle . read . head)
where
triangle n = unlines $ map mkline $ [1..n]
mkline i = unwords $ take i $ drop (i `mod` 2) $ altern
altern = "0" : "1" : altern
class Triangle {
public static String mkline(int n) {
StringBuilder line = new StringBuilder(Integer.toString(n % 2));
for (int i = 1; i < n; i++)
{ line.append(" " + Integer.toString((n + i) % 2)); }
return line.toString();
}
public static String mktriangle(int n) {
StringBuilder triangle = new StringBuilder();
for (int i = 1; i <= n; i++)
{ triangle.append(mkline(i) + "\n"); }
return triangle.toString();
}
public static void main(String[] args) {
System.out.print(mktriangle(Integer.parseInt(args[0])));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment