1.CS100 - 1 - First Week
1.CS100 - 1 - First Week
Welcome to NITK
CS100 / CS101
Python Programming,
Python Programming Lab
Outline
Where can you find computers?
●
What comes to your mind when someone says
“Computers”?
Where can you find computers?
●
Everywhere
Computer
Computer
What can a Computer do?
●
How does a computer work?
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
What can a Computer do?
●
Basic Arithmetic, Digital Logic
– + - * / % and or
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
Repeat these tasks (nearly) infinitely
●
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
Repeat these tasks (nearly) infinitely
– Think multiplication tables
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
Repeat these tasks (nearly) infinitely
●
Remember values of computations
●
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
Repeat these tasks (nearly) infinitely
●
Remember values of computations
– Memory
What can a Computer do?
●
Basic Arithmetic, Digital Logic
●
Repeat these tasks (nearly) infinitely
●
Remember values of computations
●
Computers only know what you tell them
– No intuition, No IQ, No problem solving skills, ...
CS100 and CS101 will teach you
Problem
Problem
Solving
Solving
Knowledge
Knowledge of
of Programming
Programming
Concepts
Concepts Skill
Skill
Problem Solving
●
Example problems.
Problem Solving
●
Find the next largest prime number.
Mersenne Prime
●
Q: Find the square root of the input
●
Expected Output:
–
Square Root of a Number
●
Given Input: Number
– Eg. 16
●
Q: Find the square root of the input
●
Expected Output:
– 4
Steps to calculate root of a number
Calculate root of a number, n
1. Start with an arbitrary guess, g
2. Calculate g*g
3. If g*g is equal to n, we’re done! Output g
4. If not, use (g + n/g)/2 as the next guess.
5. Go to Step 2 and repeat
Calculate root of 16
g g*g n/g (g + n/g)/2
Calculate root of 16
g g*g n/g (g + n/g) / 2
500 25000 0.032 250.016
250.016 62508.000256 0.063995904 125.039997952
125.039997952 15635.001087869 0.127959055 62.583978504
62.583978504 3916.754365339 0.255656486 31.419817495
31.419817495 987.204931407 0.509232748 15.964525121
15.964525121 254.866062353 1.002222107 8.483373614
8.483373614 71.967627873 1.886042125 5.18470787
5.18470787 26.881195693 3.085998363 4.135353117
4.135353117 17.101145399 3.869077089 4.002215103
4.002215103 16.017725731 3.997786123 4.000000613
4.000000613 16.000004904 3.999999387 4
4 16 -- --
Calculate root of a number, n
1. Start with an arbitrary guess, g
2. Calculate g*g
3. If g*g is equal to n, we’re done! Output g
4. If not, use (g + n/g)/2 as the next guess.
5. Go to Step 2 and repeat Let
Letususrepresent
representthis
this
Computational
ComputationalSolution
Solution
in
inaastandard
standardform
form
Flowchart
Start
End
Flowchart
Input n
Flowchart
Calculate g*g
Flowchart
g*g False
Equals
n?
True
Calculate root of n - Flowchart
Start
End
Calculate root of n – Flowchart
Start
1. Start with an arbitrary
guess, g Input n
2. Calculate g*g
Pick an arbitrary Calculate
3. If g*g is equal to n, number, g (n + n/g)/2
True
Output g End
Square Root of a Number
●
● Given
Given Input:
Input: Number
Number
●
● Given
GivenInput:
Input:Positive
Positivewhole
wholenumber
number
–– Eg. 16
–– Eg.
Eg.16
16 Eg. 16
●
Q:
Q:Find
Findthe
thesquare
squareroot
rootof
ofthe
theinput
input
Q:
Q: Find
Findthe
thesquare
square root
root of
●
●
●
of with
withananaccuracy
accuracyof
of+/-
+/-0.0001
0.0001
the
the input
input
● Accepted outputs:
●
Accepted outputs:
●
● Expected
ExpectedOutput:
Output: –– 4.0, 4.0001, 4.00005, ...
4.0, 4.0001, 4.00005, ...
–– 44 Outputs
OutputsNot
NotAccepted:
Accepted:4.1,
4.1,4.01,
●
●
4.01,
4.001
4.001
Homework
●
Draw a flowchart for your favorite activity this
week
Class 3
Check if 100 is a prime number
●
Input: 100
●
Q: Check if the given input is a prime number
●
Output: Yes / No
Draw the Flowchart
Check if 101 is a prime number
●
Input: 101
●
Q: Check if the given input is a prime number
●
Output: Yes / No
Draw the Flowchart
How many primes in (1, 100)
●
Inputs: 1, 100
●
Q: Count the number of prime numbers in the
given range
●
Output: 25
Count Primes in (1,100)
Count Primes in (1,100) – Flowchart
Extra Slides
Problem Solving
●
Given an engineering challenge, produce a
working solution; express the solution as
components of a machine
– Solution for a mechanical engineering challenge –
moving components (gears, pistons, ...), controllers
(electrical or microcontrollers)
Square Root of a Number
●
Given Input: Positive whole number
– Eg. 16
●
Q: Find the square root of the input with an accuracy of
+/- 0.0001
●
Accepted outputs:
– 4.0, 4.0001, 4.00005, ...
●
Outputs Not Accepted: 4.1, 4.01, 4.001