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

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 

Recently uploaded (20)

REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 

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