📘 Testing prime numbers using Perl 6

Decide if the given number is a prime number. Prime numbers are those that can be divided only by 1, and by themselves. Perl 6 provides built-in support, using the is-primeroutine, for checking if the number is prime. There are two ways of using it. First, as a built-in function: say ‘Prime’ if is-prime(17); Second, as a … Continue reading “📘 Testing prime numbers using Perl 6”

📘 Multiplying big numbers using Perl 6

Create a program to multiply very big integer numbers. In Perl 6, the support for big numbers is built-in: the Intclass allows arbitrary precision. You do not have to do any extra work. You just take the numbers and multiply them, as shown in the example (numbers should be written on one line, and without spaces, … Continue reading “📘 Multiplying big numbers using Perl 6”

📘 Compare numbers approximately using Perl 6

Compare the two non-integer values approximately. Comparing non-integer numbers, which are represented as floating-point numbers is often a task that requires approximate comparison. In Perl 6, there is the =~= operator, called the approximately-equal operator, which checks if its operands are close enough to each other. say 1/1000 =~= 1/1001;         # Falsesay 1/1E20 =~= 1 / (1E20 + … Continue reading “📘 Compare numbers approximately using Perl 6”

📘 Checking odd and even numbers in Perl 6

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, … Continue reading “📘 Checking odd and even numbers in Perl 6”

📘 Computing powers of two using Perl 6

Print the first ten powers of two. The naïve loop for calculating powers of two can be created similar to the solution of the Task 22, Print squares: say 2 ** $_ for 0..9; It prints the values 1, 2, 4, etc. up to 512. In Perl 6, there’s another way of generating sequences with the defined … Continue reading “📘 Computing powers of two using Perl 6”

📘 Print squares using Perl 6

Print the squares of the numbers from 1 to 10. To print the squares for a range of numbers, a loop is required. In Perl 6, if the body of the loop is simple, you can use the postfix notation: say $_ ** 2 for 1..10; The $_ variable is a loop variable, which receives the … Continue reading “📘 Print squares using Perl 6”

📘 Computing Fibonacci numbers using Perl 6

Print the Nth Fibonacci number. Fibonacci numbers are defined by the recurring formula: fn = fn-1 + fn-2. In Perl, you can assign two values at a time (see Task 48, Swap two values). You can use that technique for calculating the next Fibonacci number from the previous two. To bootstrap the algorithm, the two first … Continue reading “📘 Computing Fibonacci numbers using Perl 6”

📘 Computing factorial! using Perl 6

Print the factorial of a given number. By definition, the factorial of a positive integer number N is a product of all the integers numbering from 1 to N, including N. In Perl 6, this can be easily expressed with the use of a reduction operator: my $n = 5;my $f = [*] 1 .. $n;say $f; The … Continue reading “📘 Computing factorial! using Perl 6”

📘 The number π in Perl 6

Print the value of π. The value of  is accessible with no additional modules: say π; This instruction, not a surprise, prints the desired value: 3.14159265358979 As you may have noticed, a non-ASCII character was used in the code. Perl 6 assumes that the source code is encoded as UTF-8 by default. Instead, a non-Unicode version can be … Continue reading “📘 The number π in Perl 6”