l03 simple algorithms fact+prime numbers
TRANSCRIPT
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
1/18
Elementary numerical analysis
Samples of algorithms
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
2/18
Elementary numerical analysis
Factorial computing
But factorial value n! growths very quickly when increasingn, 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
A problem: compute n! for a given integern:
There are several ways to compute n!For example, usingcycle orrecursion.
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
!!!n
i
inn1
...21!
"
!
! 0!1
01
! nnn
n
n
Factorial ofn gives a number of ways to arrange n distinct objects
into a sequence. It is used in combinatorics.
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
3/18
Elementary numerical analysis
Factorial computing#include
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
{
int i = 2;
while (i
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
4/18
Elementary numerical analysis
Factorial computing#include
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 returnn*r_fact(n-1);
}
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
5/18
Elementary numerical analysis
Factorial computing
#include
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
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));
}
}
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
6/18
Elementary numerical analysis
Factorial computingStirling's approximation (or Stirling's formula) :
n
e
nnn
} T2!Considering the main term is enough sometimes:
4727761475687486.12!
7620308479001603.12!
479001600!12
}
}
!
(1)
(2)
(1)
(2)
(exact)
%697.0
%10854.77
!
!
H
H
328932923.50617514!
727785824.00000344!
244!
}
}
!
(1)
(2)
(exact)
%101.2%10447.1
5
!
!
H
H
36
36
36
1014179588.6614183833!
1003139948.6833176233!
1088118868.68331761!33
}
}
!
(1)
(2)
(exact)
%253.0
%1073.1 8
!
!
H
H
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
7/18
Elementary numerical analysis
A person
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
8/18
Elementary numerical analysis
A person
A postal stamp issued
6.09.1983 in the Soviet Union,commemorating al-Khwrizm's
(approximate) 1200th birthday.
AbuAbdullah Muhammadbin
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.
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
9/18
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) guarismoand 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
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
10/18
Elementary numerical analysis
Prime numbers search
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
11/18
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.
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
12/18
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 aproduct 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
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
13/18
Elementary numerical analysis
Prime numbers
The Electronic Frontier Foundation(EFF) will confer prizes of:
$50,000
to the first individual orgroup who discovers a prime number with
at least 1,000,000 decimal digits (awarded Apr. 6, 2000)
$100,000
to the first individual orgroup who discovers a prime number with
at least 10,000,000 decimal digits (awarded Oct. 22, 2009)
$150,000
to the first individual orgroup who discovers a prime number with
at least 100,000,000 decimal digits
$250,000
to the first individual orgroup who discovers a prime number with
at least 1,000,000,000 decimal digits
http://w2.eff.org/awards/coop-prime-rules.php
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
14/18
Elementary numerical analysis
Prime numbers
How to find prime numbers?
Title: Bad method of searching prime numbers
Input data: IntegerN
Required: Prime numbers between 2 and N
Start
CYCLE: i = 2, , ndivisions = 0;
CYCLE: j = 1,, iIFi%j == 0,
THEN divisions ++;END IFIF divisions > 2
THEN break internal cycle;END IF
END OFCYCLEif divisions == 2
THEN print i // i is a prime numberEND OFCYCLE
End
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
15/18
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 byEratosthenes, an ancient Greek
mathematician.
None of his mathematical works survive, and the sieve was described andattributed toEratosthenes in the Introduction to Arithmetic by
Nicomachus (another one important ancient mathematician).
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
16/18
Elementary numerical analysis
Prime numbers
3. Strike from the list all multiples ofp less than or equal to N.
4. Find the first number remaining on the list greater than p (the next prime
and replacep with this number.
5. Repeat steps 3 and 4 until is greater than n.2
p
2. Initially, let , the first prime number2!p
1. Create a list of integers form 2 toN
:N
,...,4,3,2
6. All the remaining numbers on the list are prime.
The complexity of the algorithm is with a memory
requirement of .
nnnO logloglog nO
Algorithm:
Sift the Twos and sift the Threes,
The Sieve of Eratosphenes.When the multiple sublime,
The numbers that remain are Prime
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
17/18
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 after2 is 3; strike the multiples of3 from the list:
2 3 5 7 11 13 17 19 23 25 29
The first number in the list after3 is 5; strike the remaining multiples of5:
2 3 5 7 11 13 17 19 23 29
The first number in the list after5 is 7, but 7 squared is 49 which is greater than 30,so the process is finished.
See also: sieve ofSundaram,Sieve of Atkin,Eulers sieve
-
8/8/2019 L03 Simple Algorithms Fact+Prime Numbers
18/18
Elementary numerical analysis
Algorithm analysis: Homework 2
Write a program computing prime numbers between 1 and givenn
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.
C:\>program.exe
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)
1
Thank you!
There are 4 prime numbers between 1 and 9:
2, 3, 5, 7
Press any key to continue
comput
er screen
Example of program execution result:Due to 28 Sept 2010