(10) Defining and Using Functions

(10) Defining and Using Functions - 1 Delegating Tasks...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Delegating Tasks Subprograms Nathan Friedman 2008 Prime Numbers Applications that use cryptography, random number generators, simulation, hashing and others require prime numbers 2008 Subprograms 2 What is a prime number? A positive integer is prime if it has no proper divisors (i.e. the only divisors are 1 and the number itself) For example 2, 3, 5, 29, 67, 83, 97, 2 1257787-1 Problem Find all prime numbers less than a given value, n 2008 Subprograms 3 2 Problem Find all prime numbers less than a given value, n Some facts: 2 is a prime number 2008 Subprograms 4 Problem Find all prime numbers less than a given value, n Some facts: 2 is a prime number 2008 Subprograms 5 All primes greater than 2 are odd Problem Find all prime numbers less than a given value, n Some facts: 2 is a prime number 2008 Subprograms 6 All primes greater than 2 are odd If we could determine whether a given number is prime or not, we could write a program that tests each odd number between 2 and n 3 Problem Find all prime numbers less than a given value, n Some facts: 2 is a prime number 2008 Subprograms 7 All primes greater than 2 are odd If we could determine whether a given number is prime or not, we could write a program that tests each odd number between 2 and n Of course, n must be positive and 2 or greater High Level Solution 1. We begin by assuming we are able to determine whether a given number is prime or not 2. We input n and test to see whether it is within a 2008 Subprograms 8 valid range (input validation). We assume we are able to validate the input. 3. We then test each odd number between 2 and n using for primality. We use a loop structure to process each of these numbers. Top Down Approach This is an example of Top-Down Programming We design a solution that assumes we are able to obtain the solution to various subproblems 2008 Subprograms 9 This high level solution makes two assumptions 1. We know how to obtain and validate the input 2. We know how to determine that a number is prime 4 PROGRAM Primes IMPLICIT NONE INTEGER :: Range, Number, Count integer :: GetNumber LOGICAL :: Prime Range = GetNumber() Count = 1 WRITE(*,*) "Prime number #", Count, ": ", 2 DO Number 3 Range 2 2008 Subprograms 10 DO Number = 3, Range, 2 IF ( Prime(Number) ) THEN Count = Count + 1 WRITE(*,*) "Prime number #", Count, ": ", NUMBER END IF END DO WRITE(*,*) "There are ", Count, " primes between 2 and ", Range END PROGRAM Primes The Missing Pieces This solution made two assumptions 1. We know how to obtain and validate the input 2. We know how to determine that a number is 2008 Subprograms 11 prime We used functions to write the program based on these assumptions We have to define the functions we used Obtain and Validate the Input INTEGER FUNCTION GetNumber() IMPLICIT NONE INTEGER :: N WRITE(*,*) "What is the range ? " DO READ(*,*) N 2008 Subprograms 12 READ( , ) N IF (N >= 2) EXIT WRITE(*,*) "The range value must be >= 2. "& "Your input is ", N WRITE(*,*) "Please try again:" END DO GetNumber = N END FUNCTION GetNumber 5...
View Full Document

Page1 / 20

(10) Defining and Using Functions - 1 Delegating Tasks...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online