Ask your own question, for FREE!
Mathematics 16 Online
OpenStudy (raffle_snaffle):

matlab help

OpenStudy (raffle_snaffle):

@mjdennis

OpenStudy (raffle_snaffle):

%% Problem 9.5 %{ Use the primes function to create a list of all the primes below 100. Now use a for loop to multiply adjacent values together. For example, the first four prime numbers are 2 3 5 7. Your calculation would be 2*3 3*5 5*7 which gives 6 15 35 %} % defing variables a = 100; c9p5 = primes(100); for k = 1:length(c9p5) a = c9p5(k) * end

OpenStudy (mjdennis):

OK, so if your list were only 3 items long ([2 3 5]), do you need to execute the loop 3 times? k=1: 2*3 k=2 3*5 K=3 5*oops-i-am-out-of-items... Somehow you need to do one fewer steps...

OpenStudy (raffle_snaffle):

Well the question asks all the primes below 100

OpenStudy (raffle_snaffle):

never mind I figured it out

OpenStudy (mjdennis):

Correct, and c9p5 = primes(100); gives you the primes below 100, I think. But, to find the problem in your code, let's model the problem as the primes less than 6 and use primes(6), length(c9p5) is now 3, right? BUT DO WE DO 3 MULTIPLICATIONS to solve? No, only 2. See my last reply. So instead of using "length(c9p5)' how do you get a loop one iteration shorter.

OpenStudy (mjdennis):

Oops, too late...

OpenStudy (raffle_snaffle):

here I will show you what I have

OpenStudy (raffle_snaffle):

% defing variables p = primes(100); a = zeros(1, length(p) - 1); for i = 1:length(p) - 1 a(i) = p(i) * p(i + 1); end

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!