Recitation 07 Problems- Solutions

# Recitation 07 Problems- Solutions - 14:440:127- Fall 09...

This preview shows pages 1–4. Sign up to view the full content.

14:440:127- Fall ‘09 Recitation Problems 07- Solutions 1) Write a while loop that continually asks the user to input a number. Each time he/she enters a positive number, you should display that number to the screen. Once the enter a number that’s not positive, the loop should stop, and you should display “Loop Stopped” to the screen. x=input( 'Enter a number' ); while (x>0) disp(x) x=input( 'Enter a number' ); end disp( 'Loop stopped' ) 2) Write a while loop that finds the smallest 8 digit prime number. Then, instead write that loop as a for loop (using the break command). n=10000000; % smallest 8 digit number while (~isprime(n)) % loop until you find a prime n=n+1; end disp(n) for n=10000000:99999999 if (isprime(n)) break end end disp(n) 3) Write a loop to create the following matrix: 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 124 128 132 136 140 144 148 152 156 160 164 168 172 176 180 184 188 192 196 200 204 208 212 216 220 224 v=4:4:32; for r=1:7 H(r,:)=v+32*(r-1); end disp(H)

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

View Full Document
% alternate solution n=4; for r=1:7 for c=1:8 L(r,c)=n; n=n+4; end end disp(L) 4) Rewrite the following code using a while loop for k=1:10 x= 50- k^2 if(x<0) break end y= sqrt(x) end k=1; while (k<=10) x= 50- k^2 if (x<0) break end y= sqrt(x) k=k+1; end % had there been a semicolon following x=50-k^2; k=1; while ((50-k^2)>=0) y=sqrt(50-k^2) k=k+1; end 5) Write a while loop that calculates the sum of the first 500 prime numbers in two ways: First, by counting up from 2, checking if each number is prime, and if so adding it to the sum. Once you’ve found 500, stop. Then, solve the problem by writing a loop that successively uses the primes( ) function to create lists of prime numbers until there are at least 500 entries. At that point, sum the first 500 elements of that vector. Try and think of an intelligent way to decide the inputs to the primes( ) function in order to minimize the number of times you have to call that function yet don’t wildly overshoot (and have a list of 2000 entries).
x=2; p=[]; while (length(p)<500) if (isprime(x)) p(length(p)+1)=x; end x=x+1; end disp(sum(p)) % alternate solution x=1000; p=primes(x); while (length(p)<500) x=x*4*length(p)/500; p=primes(x); end disp(sum(p(1:500))) 6) Calculate the total resistance of electrical resistors as follow: First, prompt the user to enter “1” if the resistors are in series, and “2” if they are in parallel. Recall that you calculate the total resistance by summing those in series, or by calculating 1/Rtot =

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/11/2010 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.

### Page1 / 11

Recitation 07 Problems- Solutions - 14:440:127- Fall 09...

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

View Full Document
Ask a homework question - tutors are online