Raku Books / Using Raku / Numbers / Using numbers
24. Odd and even numbers
Print the first ten odd numbers. Print the first ten even numbers.
Odd numbers are those that have a remainder after division by 2. This fact can be directly exploited in filtering the numbers and printing only those that match this definition.
.say if $_ % 2 for 1 .. 20;To print even numbers, negate the condition by choosing another
keyword: unless, instead of if:
.say unless $_ % 2 for 1 .. 20;Numbers can be filtered using the grep built-in
function:
.say for grep {$_ % 2}, 1..20;For the odd numbers, negate the condition by using the divisibility
operator, which returns True when its first operand is
divisible by the second with no remainder:
.say for grep {$_ %% 2}, 1..20;Another interesting approach is using a sequence. Show the first elements of it, and the rest are generated automatically:
my @odd = 1, 3 ... *;
say @odd[^10];To print the even numbers, change the sample:
my @even = 2, 4 ... *;
say @even[^10];