Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using MATLAB write the following: Write a program, SumUpTo.m, that uses a for lo

ID: 2081518 • Letter: U

Question

Using MATLAB write the following:

Write a program, SumUpTo.m, that uses a for loop to calculate the sum of the positive integers up to N. Set the value of N near the beginning of the program. (N = 100 was Gauss's famous trick.) That is. find and print: P (n) = sigma^n _k = 1 k Write a program, Find Primes. m, that examines each integer from 1 to N to see if the number is a prime. If it is prime (and only then), print out the number. Use the MATLAB function is prime (k). which returns the value true if k is prime? The program should print out all the primes less than or equal to N. Use N = 100 as a test case. >> Find Primes N = 100 Found Prime: 2 Found Prime: 3 Found Prime: 5 Found Prime: 7 Found Prime: 11 (etc.)

Explanation / Answer

CODE

This is the code where user has to input the value of N from command window.

(a)

function SumUpTo(N)
clc;
sum=0;
for i=1:N
sum=sum+i;
end
sum
end

(b) If the user wants that the the value of N should be enetered in the program only, then the code would be modified as follows

function SumUpTo(N)
clc;
N=100;
sum=0;
for i=1:N
sum=sum+i;
end
sum
end

The answer of above code is 5050

CODE(B)

function FindPrimes()
clc;
N=100;
q=zeros(N,1)
for i=1:N
k=isprime(i);
if k==1
disp('Found Prime: ')
i
end
end

OUTPUT

FindPrimes

N =

100

Found Prime:

i =

2

Found Prime:

i =

3

Found Prime:

i =

5

Found Prime:

i =

7

Found Prime:

i =

11

Found Prime:

i =

13

Found Prime:

i =

17

Found Prime:

i =

19

Found Prime:

i =

23

Found Prime:

i =

29

Found Prime:

i =

31

Found Prime:

i =

37

Found Prime:

i =

41

Found Prime:

i =

43

Found Prime:

i =

47

Found Prime:

i =

53

Found Prime:

i =

59

Found Prime:

i =

61

Found Prime:

i =

67

Found Prime:

i =

71

Found Prime:

i =

73

Found Prime:

i =

79

Found Prime:

i =

83

Found Prime:

i =

89

Found Prime:

i =

97