Namul is a programming language for competitive programming.
A format string is an expression that prints formatted text.
`Hello, World!`;
Use input statement to define and read into variables.
|i32 A, i32 B|
i32 answer = A + B;
`$answer`;
You don't need variables to store temporary result.
|i32 A, i32 B|
`$`, A + B;
Repeat blocks T
times.
|i32 T|
rep T {
|i32 A, i32 B|;
`$\n`, A + B;
}
Comparisons can be chained. Format strings are expressions to allow use in conditional operations.
|i32 year|
year % 4 == 0 != year % 100 || year % 400 == 0 ? `1` : `0`;
Declarations and assignments matches patterns.
|i32 a, i32 b|
(i32 c, i32 d) = (a, b);
while d != 0 {
(c, d) = (d, c % d);
}
d = a / c * b;
`$c\n$d`;
Or you can use classic recursive function:
|i32 a, i32 b|
i32 c = gcd(a, b);
i32 d = a / c * b;
`$c\n$d`;
fn gcd(i32 a, i32 b) i32 {
b == 0 ? a : gcd(b, a % b)
}
Of course, you don't need to define your function before its call.
Read N
numbers, count occurrences of v
.
You can read into slices.
|i32 N|
[i32; 100] A;
|A[0..N], i32 v|
i32 count = 0;
rep i, N {
count += A[i] == v ? 1 : 0;
}
`$count`;