Write an integer number below one million in words. Human languages have many inconsistencies, especially in the most frequent constructs. Spelling numbers seems to be a simple task, but due to a number of small differences, the resulting program is quite big. The program is listed on the next page. Let’s discuss the algorithm first. … Continue reading “📘 Spelling numbers using Perl 6”
Category: Chapter 2. Numbers
📘 Convert to Roman numerals using Perl 6
Convert an integer number to a Roman numerals string. Roman numbers are not a direct translation of the decimal system. In this task, we assume that the number is not morethan 3999, which is the maximum a regular Roman number can reach. Let’s use the algorithm that keeps the table of pre-calculated sequences of Roman letters … Continue reading “📘 Convert to Roman numerals using Perl 6”
📘 Compose the largest number using Perl 6
Given the list of integers, compose the largest possible number by concatenating them. This task requires working with the same data as with numbers and strings. To compose the largest possible number out of a list of integers, we need to reorder them so that the largest numbers come first. The easiest way to achieve … Continue reading “📘 Compose the largest number using Perl 6”
📘 Bit counter written in Perl 6
Count the number of bits set to 1 in a binary representation of a positive integer number. There are two approaches to this task: either treat the binary sequence as a string or count real bits in the machine representation of the number. In Perl, both approaches are fine. We restrict ourselves to the positive … Continue reading “📘 Bit counter written in Perl 6”
📘 Computing the sum of digits using Perl 6
Calculate the sum of digits of a given number. The solution of this task is based on the splitmethod that you can call on any object that is convertible to strings. The following code takes an integer number and splits it into separate characters, one per digit. Then the reduction operator adds up all the digits, … Continue reading “📘 Computing the sum of digits using Perl 6”
📘 Presenting integers as binary, octal, and hex using Perl 6
Print a given integer number in the binary, octal, and hexadecimal representations. On an integer object, call the basemethod with the corresponding number: say 42.base(2); # 101010say 42.base(8); # 52say 42.base(16); # 2A Alternatively, use the fmt method, which is defined for integers and accepts the formatting string in the printfformat: my $int = 42;say $int.fmt(‘Hex: %x’); # Hex: 2asay $int.fmt(‘Oct: %o’); # … Continue reading “📘 Presenting integers as binary, octal, and hex using Perl 6”
📘 Converting binary to integer using Perl 6
Convert a binary number to a decimal integer. Perl is good at its ease of switching between numerical and string representation of the same data. This task is an example where this feature can be used. The idea is to take a binary number, treat it as a string, prepend the 0bprefix indicating the binary value, … Continue reading “📘 Converting binary to integer using Perl 6”
📘 ‘Guess the number’ in Perl 6
Write a program that generates a random integer number 0 through 10 and asks the user to guess it, saying if the entered value is too small or too big. First, a random number needs to be generated. In Perl 6, the randroutine can be called on an integer object, and it returns a random floating-point … Continue reading “📘 ‘Guess the number’ in Perl 6”
📘 All Unicode digits in Perl 6
Print all Unicode digits. Perl 6 has the best support of Unicode among the modern programming languages. When talking about digits, it is worth remembering that the Unicode standard marks as digits much more than the regular ten characters used in English, for example. Let us iterate over the whole range of codepoints and select … Continue reading “📘 All Unicode digits in Perl 6”
📘 Implementing the Monte Carlo method using Perl 6
Calculate the area of a circle and the volume of a sphere of radius 1 using the Monte Carlo method. The Monte Carlo method is a statistical method of calculating data whose formula is not known. The idea is to generate a big number of random numbers and see how many of them satisfy the … Continue reading “📘 Implementing the Monte Carlo method using Perl 6”
📘 Working with polar coordinates in Perl 6
Convert the Cartesian coordinates to polar and backward. Polar coordinates are a convenient way of representing points on a surface with the two values: distance from the centre of coordinates and the angle between the vector and the pole axis. The conversion formulae between the Cartesian and polar systems,which is valid for positive x and y, are … Continue reading “📘 Working with polar coordinates in Perl 6”
📘 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”