You are on page 1of 18

Samples of algorithms

Elementary numerical analysis


Factorial computing
A problem: compute n! for a given integer n:
n
 1 n0

n! 1  2  ...  n 
i 1
i n! 
n n  1! n  0
Factorial of n gives a number of ways to arrange n distinct objects
into a sequence. It is used in combinatorics.
There are several ways to compute n! For example, using cycle or recursion.
But factorial value n! growths very quickly when increasing n, for example:
64! =
126,886,932,100,000,000,000,
000,000,000,000,000,000,000,
000,000,000,000,000,000,000,
000,000,000,000,000
Actually, a set of possible values of the input argument is very small.
For the most systems, this set is numbers from 1 to 20 (or even less).
20! = 20 * 19 * 18 *...* 2 * 1 = 2,432,902,008,176,640,000
Elementary numerical analysis
Factorial computing
#include <stdio.h>
long r_fact(int n);
long fact(int n);

void main (void)


{
int N;
while (1)
{
printf("\n\nEnter integer N: ");
scanf("%d", &N);
printf("\n %d! = %ld", N, fact(N));
}
}
long fact(int n)
{
long factorial = 1;
if ((n==1) || (n==0)) return 1;
if (n < 1) {printf ("\nFor n < 0 n! is not defined!\n"); return 0;}
else
{ Add code here
int i = 2;
while (i <= n) { factorial*=i; i++;}
}
return factorial;
}
Elementary numerical analysis
Factorial computing
#include <stdio.h>

long r_fact(int n); // recursive calculation


long fact(int n);

void main (void)


{
int N;
while (1)
{
printf("\n\nEnter integer N: ");
scanf("%d", &N);
printf("\n %d! = %ld", N, r_fact(N));

}
}

long r_fact(int n)
{
if ((n==1) || (n==0)) return 1;
if (n < 0) {printf ("\nFor n < 0 n! is not defined!\n"); return 0;}

else return n*r_fact(n-1);


}

Elementary numerical analysis


Factorial computing
void main (void)
{
while(1)
{
int N;
printf("\nEnter integer N < 17\n");
scanf("%d", &N); if (N >= 17 || N < 0) continue;
printf ("%d! = %ld\n", N, t_fact(N));
}
}
#include <stdio.h>

long t_fact(int n)
{
long factorials[17] = { 1,1,2,6,24,120,720,5040,40320,362880,3628800,
39916800,479001600,6227020800,87178291200,
1307674368000,20922789888000 };
return factorials[n];
}

Sometimes it is more efficient to store predefined data


instead of computing them

Elementary numerical analysis


Factorial computing
Stirling's approximation (or Stirling's formula) :

(1)
n
n
Considering the main term is enough sometimes: n! 2n   (2)
e

4! 24 (exact)
4! 24.00000347277858 (1)   1.447  105%
4! 23.50617513289329 (2)   2.101%

12! 479001600 (exact)


12! 479001603.7620308 (1)   7.854  10 7 %
12! 475687486.4727761 (2)   0.697%

33! 8.683317618811886  1036 (exact)

33! 8.683317620313994  1036 (1)   1.73 108%


33! 8.661418381417958  1036 (2)   0.253%
Elementary numerical analysis
A person

Elementary numerical analysis


A person
Abu Abdullah Muhammad bin
Musa al-Khwarizmi (AL-KHOREZMI)
(783-850)

A great Persian mathematician,


astronomer and geographer, a scholar in the
House of Wisdom in Baghdad.

He is considered the founder of algebra, sharing


his credit with Diophantus.
A postal stamp issued
6.09.1983 in the Soviet Union,
commemorating al-Khwārizmī's
(approximate) 1200th birthday.

Elementary numerical analysis


A person
The words “algorism” and “algorithm” stem from
“Algoritmi”, the Latin form of his name.
His name is the origin of (Spanish) “guarismo”
and of (Portuguese) “algarismo”,
both meaning “digit”.

“Algebra” is derived from “al-jabr”,


one of the two operations he used to solve
quadratic equations.

A monument to
Al-Khorezmi
in Tehran University
Elementary numerical analysis
Prime numbers search

Elementary numerical analysis


Prime numbers
In mathematics, a prime number (or a prime) is a natural number which has
exactly two distinct natural number divisors: 1 and itself.

The first twenty-six prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,


43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101

There is no known formula yielding all primes. The distribution of primes


in the large can be modelled.
Despite being intensely studied, many fundamental questions around
prime numbers remain open.

Elementary numerical analysis


Prime numbers
There are infinitely many prime numbers.

Euclid: Let us assume that the number of primes is finite. Let us make a
product of all primes and then add a unit. The result cannot be divided by
any number of the finite set of primes, because the residual is a unit for all
these numbers. Hence, the result must be divided by a prime number which
is not included in the finite set. This is a contradiction.

Largest prime numbers


Euler: 231 − 1 = 2147483647.
UCLA (23.08.2008 ): 243112609 − 1, the number contains 12 978 189 digits

Elementary numerical analysis


Prime numbers
The Electronic Frontier Foundation (EFF) will confer prizes of:
$50,000
to the first individual or group who discovers a prime number with
at least 1,000,000 decimal digits (awarded Apr. 6, 2000)

$100,000
to the first individual or group who discovers a prime number with
at least 10,000,000 decimal digits (awarded Oct. 22, 2009)

$150,000
to the first individual or group who discovers a prime number with
at least 100,000,000 decimal digits

$250,000
to the first individual or group who discovers a prime number with
at least 1,000,000,000 decimal digits

http://w2.eff.org/awards/coop-prime-rules.php
Elementary numerical analysis
Prime numbers
How to find prime numbers?
Title: Bad method of searching prime numbers
Input data: Integer N
Required: Prime numbers between 2 and N
Start
CYCLE: i = 2, …, n
divisions = 0;
CYCLE: j = 1,…, i
IF i%j == 0,
THEN divisions ++;
END IF
IF divisions > 2
THEN break internal cycle;
END IF
END OF CYCLE
if divisions == 2
THEN print i // i is a prime number
END OF CYCLE

End
Elementary numerical analysis
Prime numbers
Sieve of Eratosphenes – a simple, ancient algorithm or finding all prime
numbers up to a specified integer number.
It works efficiently for the smaller primes (below 10 million).

The algorithm was created by Eratosthenes, an ancient Greek


mathematician.

None of his mathematical works survive, and the sieve was described and
attributed to Eratosthenes in the “Introduction to Arithmetic” by
Nicomachus (another one important ancient mathematician).

Elementary numerical analysis


Prime numbers
Algorithm:
1. Create a list of integers form 2 to N: 2, 3, 4, ... , N

2. Initially, let p  2 , the first prime number


3. Strike from the list all multiples of p less than or equal to N.

4. Find the first number remaining on the list greater than p (the next prime
and replace p with this number.
2
5. Repeat steps 3 and 4 until p is greater than n.

6. All the remaining numbers on the list are


prime.
Sift the Twos and sift the Threes,
The Sieve of Eratosphenes.
When the multiple sublime,
The numbers that remain are Prime

The complexity of the algorithm is O  n log n  log log n   with a memory


requirement of O n  .
Elementary numerical analysis
Prime numbers
Example
First generate a list of integers from 2 to 30:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Strike (delete) the multiples of 2:


2 3 5 7 9 11 13 15 17 19 21 23 25 27 29

The first number in the list after 2 is 3; strike the multiples of 3 from the list:
2 3 5 7 11 13 17 19 23 25 29

The first number in the list after 3 is 5; strike the remaining multiples of 5:
2 3 5 7 11 13 17 19 23 29

The first number in the list after 5 is 7, but 7 squared is 49 which is greater than
30, so the process is finished.
See also: sieve of Sundaram, Sieve of Atkin, Euler’s sieve

Elementary numerical analysis


Algorithm analysis: Homework 2
Write a program computing prime numbers between 1 and given n
User enters an integer number n and the way of output (screen or file).
The program computes prime numbers between 1 and given n and prints them
on a screen or to a file.
Provide all necessary data checks.
Due to 28 Sept 2010
Example of program execution result:

C:\>program.exe computer screen


Welcome to the Prime Numbers Finder v.1.0!
Enter integer n: 9
Choose output:
Press 1 if you want to print numbers on a screen or
press 2 for writing numbers to a file (file primes.dat will be created)

Thank you!
There are 4 prime numbers between 1 and 9:
2, 3, 5, 7

Press any key to continue…

Elementary numerical analysis

You might also like