# dan-barbulescu-dev / pascal-tutorials

Pascal code samples
Pascal Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information. LICENSE README.md digit_sum.pas file_put_contents.pas gcd.pas is_palindrome.pas is_prime.pas pow10.pas reverse_digits.pas sieve_of_eratosthenes.pas

# Pascal tutorials (code samples)

## Sieve of Eratosthenes – finding out all prime numbers up to N

This Pascal program finds out all the prime numbers up to a number N, by using a very efficient algorithm called The Sieve of Eratosthenes. How it works in short is that it goes through each number up to N, and if that number is prime, it marks all of its multiples as not prime. Thus, only the prime numbers are left unmarked and easy to recognize.

## Pascal function that tests if a number is prime

is_prime.pas - The function tests if a number is prime by looking for possible divisors between 2 and the square root of the number. There is no need to look for divisors bigger than the square root of the number, since if they exist, then there should also exist another divisor, which is smaller than the square root, and with which if multiplied it would give the original number.

## Pascal function that calculates the greatest common divisor of two numbers (using Euclid’s algorithm)

gcd.pas - These functions calculate the greatest common divisor of two numbers, using Euclid’s Algorithm.

pow10.pas

## Pascal function that tests if a number is a palindrome

is_palindrome.pas - A palindrome is a number that is equal to its reverse (if you spell it backwards it remains the same number). The function that that does the testing is is_palindrome(). This function uses the auxiliary function reverse_digits().

## Pascal function that reverses the digits of a number

reverse_digits.pas

## Pascal function that calculates the sum of the digits of a number

digit_sum.pas - To calculate the sum of the digits, we go through each digit by successively dividing the number by 10, and extracting each digit using the mod operator.

## Pascal procedure that writes string to file

file_put_contents.pas - If you ever need to write a string variable quickly to a file, here is a function that works similarly to PHP’s file_put_contents() and to C#’s System.IO.File.WriteAllText().

You can’t perform that action at this time.