SlideShare a Scribd company logo
1 of 19
1
—Team GCC
2
1
2
4
5
6
bool isPrime (int n)
{
for (int i=2; i<n; i++)
if (n%i==0)
return false;
return true;
}
( Simply traverse from 2 to 𝑛 − 1 )
7
bool isPrime (int n)
{
if (n<=1) return false;
if (n==2) return true;
if (n%2==0) return false;
for (int i=3; i<n; i+=2)
if (n%i==0)
return false;
return true;
}
number not
divisible by 2 is
not divisible by
any even number
8
bool isPrime (int n)
{
for (int i=2; i*i<n; i++)
if (n%i==0)
return false;
return true;
}
( Simply traverse from 2 to 𝑛 )
ALL prime numbers are of form 6𝑘 ± 1
FACT
10
bool isPrime (int n)
{
if (i==2) return true;
if (i==3) return true;
for (int i=6; i*i<n; i+=6)
if (n%(i-1)==0)
return false;
else if (n%(i+1)==0)
return false;
return true;
}
11
Basic concept - we pick one prime number and eliminate all the
multiples of that prime number
1. Create an array of 𝑛 + 1 elements (0,1,…,n)
2. Initialise the array with 1 (consider all as prime)
3. Mark 0 & 1 as 0 (!prime)
4. Iterate array till 𝒊 = 𝟏 (prime is found)
5. Mark all consecutive multiple of p as 0 (!prime)
6. Repeat step 4 & 5 till 𝑖 ≠ 𝑛
12
void generatePrime (int n)
{
int isPrime[n+1];
for (int i=0; i<n; i++)
isPrime[i] = 1;
isPrime[0] = isPrime[1] = 0;
for (int i=2; i<n; i++)
if (isPrime[i]==1)
for (int j=i+i; j<n; j+=i)
isPrime[i] = 0;
}
Create an array of 𝑛 + 1 elements (0,1,…,n)Initialise the array with 1 (consider all as prime)Mark 0 & 1 as 0 (!prime)Iterate array till 𝑖 = 1 (prime is found)Mark all consecutive multiple of p as 0 (!prime)Repeat step 4 & 5 till 𝑖 ≠ 𝑛
13
3
14
15
What is GCD of 54 and 24?
Divisor of 54: 1, 2, 3, 6, 9, 18, 27, 54
Divisor of 24: 1, 2, 3, 4, 6, 8, 12, 24
Common divisor are: 1, 2, 3, 6
GCD (54, 24) = 6
16
Homework :D
17
https://en.wikipedia.org/wiki/Euclidean_algorithm
[video content removed]
“
18
—pGxplorer
Keep practicing,
It’s a sport worth playing!
19
THANK YOU

More Related Content

What's hot

Puzzle Solving Using Model Checking
Puzzle Solving Using Model Checking Puzzle Solving Using Model Checking
Puzzle Solving Using Model Checking Anit Thapaliya
 
Demystifying Software Interviews
Demystifying Software InterviewsDemystifying Software Interviews
Demystifying Software InterviewsMichael Viveros
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2bweldon
 
5.4 Complex Numbers
5.4 Complex Numbers5.4 Complex Numbers
5.4 Complex Numbershisema01
 
Practica 1 numeros naturales y enteros
Practica 1 numeros naturales y enterosPractica 1 numeros naturales y enteros
Practica 1 numeros naturales y enterosrjmartinezcalderon
 
C++ Question & Answer
C++ Question & AnswerC++ Question & Answer
C++ Question & Answertjunicornfx
 
Qwizdom a-level maths - equations
Qwizdom   a-level maths - equationsQwizdom   a-level maths - equations
Qwizdom a-level maths - equationsQwizdom UK
 
Lesson 4 sum and product of qe
Lesson 4  sum and product of qeLesson 4  sum and product of qe
Lesson 4 sum and product of qerina valencia
 

What's hot (20)

Complex nos demo 2
Complex nos demo 2Complex nos demo 2
Complex nos demo 2
 
Review version 4
Review version 4Review version 4
Review version 4
 
Puzzle Solving Using Model Checking
Puzzle Solving Using Model Checking Puzzle Solving Using Model Checking
Puzzle Solving Using Model Checking
 
Polynomial stations
Polynomial stationsPolynomial stations
Polynomial stations
 
Demystifying Software Interviews
Demystifying Software InterviewsDemystifying Software Interviews
Demystifying Software Interviews
 
2.2 add real numbers day 1-2
2.2 add real numbers   day 1-22.2 add real numbers   day 1-2
2.2 add real numbers day 1-2
 
Review
ReviewReview
Review
 
5.4 Complex Numbers
5.4 Complex Numbers5.4 Complex Numbers
5.4 Complex Numbers
 
Alg2 lesson 11-1
Alg2 lesson 11-1Alg2 lesson 11-1
Alg2 lesson 11-1
 
Review version 3
Review version 3Review version 3
Review version 3
 
Review version 2
Review version 2Review version 2
Review version 2
 
Practica 1 numeros naturales y enteros
Practica 1 numeros naturales y enterosPractica 1 numeros naturales y enteros
Practica 1 numeros naturales y enteros
 
Alg2 lesson 11-1
Alg2 lesson 11-1Alg2 lesson 11-1
Alg2 lesson 11-1
 
Alg2 lesson 11-1
Alg2 lesson 11-1Alg2 lesson 11-1
Alg2 lesson 11-1
 
Uts
UtsUts
Uts
 
C++ Question & Answer
C++ Question & AnswerC++ Question & Answer
C++ Question & Answer
 
Qwizdom a-level maths - equations
Qwizdom   a-level maths - equationsQwizdom   a-level maths - equations
Qwizdom a-level maths - equations
 
Polynomial stations
Polynomial stationsPolynomial stations
Polynomial stations
 
Polynomial stations
Polynomial stationsPolynomial stations
Polynomial stations
 
Lesson 4 sum and product of qe
Lesson 4  sum and product of qeLesson 4  sum and product of qe
Lesson 4 sum and product of qe
 

Similar to Number theory - Prime Numbers & GCD Algorithms

Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdf
Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdfProgram for prime numbers using Sieveof Eratosthenese Algorithm#i.pdf
Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdfannamalaiagencies
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAMSaraswathiRamalingam
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
Integers And Order of Operations
Integers And Order of OperationsIntegers And Order of Operations
Integers And Order of Operationsnickromero76
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignmentRutvik
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programsAbhishek Jena
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operationsSreedhar Chowdam
 
Semana 09 factorizacion álgebra-uni ccesa007
Semana 09   factorizacion  álgebra-uni ccesa007Semana 09   factorizacion  álgebra-uni ccesa007
Semana 09 factorizacion álgebra-uni ccesa007Demetrio Ccesa Rayme
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptMahyuddin8
 

Similar to Number theory - Prime Numbers & GCD Algorithms (20)

Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdf
Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdfProgram for prime numbers using Sieveof Eratosthenese Algorithm#i.pdf
Program for prime numbers using Sieveof Eratosthenese Algorithm#i.pdf
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
 
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAMPROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS  - SARASWATHI RAMALINGAM
PROGRAMMING IN C EXAMPLE PROGRAMS FOR NEW LEARNERS - SARASWATHI RAMALINGAM
 
Vcs5
Vcs5Vcs5
Vcs5
 
Lab Question
Lab QuestionLab Question
Lab Question
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
Nbvtalkatbzaonencryptionpuzzles
NbvtalkatbzaonencryptionpuzzlesNbvtalkatbzaonencryptionpuzzles
Nbvtalkatbzaonencryptionpuzzles
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Ada file
Ada fileAda file
Ada file
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Integers And Order of Operations
Integers And Order of OperationsIntegers And Order of Operations
Integers And Order of Operations
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignment
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs81818088 isc-class-xii-computer-science-project-java-programs
81818088 isc-class-xii-computer-science-project-java-programs
 
C programs
C programsC programs
C programs
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
PPS Arrays Matrix operations
PPS Arrays Matrix operationsPPS Arrays Matrix operations
PPS Arrays Matrix operations
 
Semana 09 factorizacion álgebra-uni ccesa007
Semana 09   factorizacion  álgebra-uni ccesa007Semana 09   factorizacion  álgebra-uni ccesa007
Semana 09 factorizacion álgebra-uni ccesa007
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 

Recently uploaded

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptSourabh Kumar
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxnuriaiuzzolino1
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...Nguyen Thanh Tu Collection
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Abhinav Gaur Kaptaan
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Celine George
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17Celine George
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...Nguyen Thanh Tu Collection
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...Sayali Powar
 

Recently uploaded (20)

Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17How to Manage Notification Preferences in the Odoo 17
How to Manage Notification Preferences in the Odoo 17
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
UNIT – IV_PCI Complaints: Complaints and evaluation of complaints, Handling o...
 

Number theory - Prime Numbers & GCD Algorithms

  • 1. 1
  • 3. 1
  • 4. 2 4
  • 5. 5
  • 6. 6 bool isPrime (int n) { for (int i=2; i<n; i++) if (n%i==0) return false; return true; } ( Simply traverse from 2 to 𝑛 − 1 )
  • 7. 7 bool isPrime (int n) { if (n<=1) return false; if (n==2) return true; if (n%2==0) return false; for (int i=3; i<n; i+=2) if (n%i==0) return false; return true; } number not divisible by 2 is not divisible by any even number
  • 8. 8 bool isPrime (int n) { for (int i=2; i*i<n; i++) if (n%i==0) return false; return true; } ( Simply traverse from 2 to 𝑛 )
  • 9. ALL prime numbers are of form 6𝑘 ± 1 FACT
  • 10. 10 bool isPrime (int n) { if (i==2) return true; if (i==3) return true; for (int i=6; i*i<n; i+=6) if (n%(i-1)==0) return false; else if (n%(i+1)==0) return false; return true; }
  • 11. 11 Basic concept - we pick one prime number and eliminate all the multiples of that prime number 1. Create an array of 𝑛 + 1 elements (0,1,…,n) 2. Initialise the array with 1 (consider all as prime) 3. Mark 0 & 1 as 0 (!prime) 4. Iterate array till 𝒊 = 𝟏 (prime is found) 5. Mark all consecutive multiple of p as 0 (!prime) 6. Repeat step 4 & 5 till 𝑖 ≠ 𝑛
  • 12. 12 void generatePrime (int n) { int isPrime[n+1]; for (int i=0; i<n; i++) isPrime[i] = 1; isPrime[0] = isPrime[1] = 0; for (int i=2; i<n; i++) if (isPrime[i]==1) for (int j=i+i; j<n; j+=i) isPrime[i] = 0; } Create an array of 𝑛 + 1 elements (0,1,…,n)Initialise the array with 1 (consider all as prime)Mark 0 & 1 as 0 (!prime)Iterate array till 𝑖 = 1 (prime is found)Mark all consecutive multiple of p as 0 (!prime)Repeat step 4 & 5 till 𝑖 ≠ 𝑛
  • 13. 13
  • 14. 3 14
  • 15. 15 What is GCD of 54 and 24? Divisor of 54: 1, 2, 3, 6, 9, 18, 27, 54 Divisor of 24: 1, 2, 3, 4, 6, 8, 12, 24 Common divisor are: 1, 2, 3, 6 GCD (54, 24) = 6