$24
7.1 Division
7.1.1 Assignment
A prime number n is a natural number (i.e. a non-negative integer) which is greater than 1 and is not divisible by any other natural number other than 1 and n. In other words, n has no non-trivial factors other than 1 and n. So for example, 2 is the smallest prime number. To test whether a natural number n is a prime number or not, you can set up a loop to test every natural number between 1 and n (i.e. natural numbers from 2 to n ¡1) one by one to see whether n is divisible by any of them and count the number of non-trivial factors for n you have seen in the whole process. If n is not divisible by any of them, the number of non-trivial factors for n is 0 and n is a prime number; otherwise n is not a prime number.
7.1.2 Instructions
You are to find all prime numbers between 1 and n, where n is entered by the user. Your program should:
prompt the user to input a natural number n,
determine and display all the prime numbers between 1 and n in increasing order, and
print the number of primes found between 1 and n.
Your program must have at least two functions:
isprime should accept one parameter, x and return 0 or 1, indicating whether or not x is prime.
divide should accept two parameters, x and y and should return the quotient and remainder (as two separate integers) found by dividing x by y.
7.2 Optimizing Multiplication and Division
7.2.1 Assignment
If you are like most people, you collect a fair amount of change in your pocket. A painless savings plan is to dump all the loose change into a jar at the end of the week. However, we’d like some idea of the amount of money we’ll have saved in a year so we can start thinking about what we want to buy.
7.2.2 Instructions
In the this exercise you will write an assembly program to estimate the yearly savings based on the amount of change saved at the end of four weeks. The amount of change saved at the end of each week will be entered as four numbers: the number of pennies, nickels, dimes, and quarters. The program should read in four sets of weekly data. The program must then use exactly four calls to printf to outpute the following results:
the total count for each type of coin,
the total in dollars and cents,
the weekly average, and
the estimated yearly savings.
Your code must:
implement division by a constant as multiplication by its reciprocal,
implement multiplication by a constant as a sequence of shift and add operations.
Example Output
Enter the number of pennies, nickels, dimes, and quarters for week 1: 1 2 3 4
2 Enter the number of pennies, nickels, dimes, and quarters for week 2: 5 6 7 8
3 Enter the number of pennies, nickels, dimes, and quarters for week 3: 1 2 3 4
4 Enter the number of pennies, nickels, dimes, and quarters for week 4: 3 2 1 0
5
Over four weeks you have collected 10 pennies, 12 nickles, 14 dimes,
7 and 16 quarters.
8
This comes to $6.10
Your weekly average is $1.52
Your estimated yearly savings is $73.20