📘 Computing standard deviation using Perl 6

For the given data, calculate the standard deviation value (sigma). Standard deviation is a statistical term that shows how compact data distribution is. The formula is the following: where N is the number of elements in the array x; is the average value (see Task 56, Average on an array). Let’s use some test data from Wikipedia and take … Continue reading “📘 Computing standard deviation using Perl 6”

📘 Computing the distance between two points using Perl 6

Calculate the distance between the two points on a surface. There are two points on a surface, each with their own coordinates, xand y. The task is to find the distance between these two points. A straightforward solution would be to use the Pythagorean theorem: my ($x1, $y1) = (10, 3);my ($x2, $y2) = (9, 1);say sqrt(($x1 … Continue reading “📘 Computing the distance between two points using Perl 6”

📘 Generating a histogram of random numbers using Perl 6

Test the quality of the random generator by using a histogram to visualise the distribution. The quality of the built-in generator of random numbers fully depends on the algorithm the developers of the compiler used. As a user, you cannot do much to change the existing generator, but you can always test if it delivers … Continue reading “📘 Generating a histogram of random numbers using Perl 6”

📘 Neumann’s random generator written in Perl 6

Implement the von Neumann’s random number generator (also known as Middle-square method). This algorithm is a simple method of generating short sequences of four-digit random integers. The method has its drawbacks, but for us, it is an interesting algorithmic task. The recipe has these steps: Take a number between 0 and 9999. Calculate the square … Continue reading “📘 Neumann’s random generator written in Perl 6”

📘 Generating random numbers using Perl 6

Generate a random number between 0 and N. Use the randmethod, which returns a random number between 0 and the value of its invocant: say 1.rand;   # between 0 and 1say 2.5.rand; # between 0 and 2.5say pi.rand;  # between 0 and 3.14… The rand method is defined in the Cool class, which is the base class for the Int, Num, and Rat types. It … Continue reading “📘 Generating random numbers using Perl 6”

📘 Divide by zero in Perl 6

Do something with the division by zero. Division by zero in itself is not an immediate error in Perl 6. The following code does not generate an exception and prints the message after the problematic division. my $v = 42 / 0;say ‘It still works!’; However, as soon as the result stored in the $v variable … Continue reading “📘 Divide by zero in Perl 6”

📘 Reducing a fraction using Perl 6

Compose a fraction from the two given integers—numerator and denominator—and reduce it to lowest terms. 5/15 and 16/280 are examples of fractions that can be reduced. The final results of this task are 1/3 and 2/35. Generally, the algorithm of reducing a fraction requires searching for the greatest common divisor, and then dividing both numerator … Continue reading “📘 Reducing a fraction using Perl 6”

📘 Finding prime factors using Perl 6

Find the prime factors of a given number. Prime factors are the prime numbers that divide the given integer number exactly. In Task 28, List of prime numbers, we’ve seen how to make a lazily evaluated list of prime numbers. This list is used in the program as a generator of the factor numbers for the … Continue reading “📘 Finding prime factors using Perl 6”

📘 Printing a list of prime numbers using Perl 6

Print thelist of the first ten prime numbers. In Task 27, Prime numbers, we’ve seen how to check if the given number is prime. To print the list of the first ten numbers, organize a lazy list.The code is quite compact and Perlish by nature: my @numbers = grep {.is-prime}, 1..*;say @numbers[^10]; The first line has to … Continue reading “📘 Printing a list of prime numbers using Perl 6”

📘 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”

📘 Finding duplicate texts using Perl 6

Find duplicate fragments in the same text. This task was dictated by the practical need when I realised that I used the same phrases in different parts of the text of this book. Some of them, like Hello, World!, are unavoidable, but it would be a great help to find the rest. Here is the full … Continue reading “📘 Finding duplicate texts using Perl 6”

📘 Finding the longest palindrome using Perl 6

Find the longest palindromic substring in the given string. The main idea behind the solution is to scan the string with a window of varying width. In other words, starting from a given character, test all the substrings of any length possible at that position. For the string $string, this is how the loops can be … Continue reading “📘 Finding the longest palindrome using Perl 6”