Question

In: Computer Science

Write two scripts(must be titled “primeSumLoopS” and “primeSumCallS”) in MATLAB that prompts the user to enter...

Write two scripts(must be titled “primeSumLoopS” and “primeSumCallS”) in MATLAB that prompts the user to enter an integer x that 2< x <10000, then the program calculates and displays the sum of the all the prime numbers that are smaller than or equal to x. For example, if x is 11, then the output is 28, which is 2+3+5+7+11. Use two methods: the 1st script uses nested loop and the 2nd script calls another function, primeF.

Solutions

Expert Solution

%% Matlab script to find sum of primes betwwen the given range using loops
clear all;
clc;
close all;
x=input('Enter X value greater than 2 and less than 10000');
s=2;
if (x>2) && (x<10000)
for i=3:x
flag=1;
for p=2:i
r=mod(i,p);
if(r==0)&&(p~=i)
flag=0;
break;
else
continue;
end
end
if flag==1
s=s+i;
end
end
  
else
disp("Enter X value greater than 2 and less than 10000")
end
disp(s)%%displaying the final sum resultThe above picture talks about matlab code finding sum of primes within given range using loops.

For the given input x=11 the output should be 28 (2+3+5+11+7).Hereby below attachment of output was done.

%%Matlab script to find out sum of primes within given range using Functions
clear all;
clc;
x=input('Enter X value greater than 2 and less than 10000')
sum=2;
for i=3:x
    flag= primeF(i);
    %disp(i)
    if(flag)
        sum=sum+i;
    end
end
disp(sum)%%display of final result
function out = isDivis(n1,sqrt_n1,div)
        if mod(n1,div) == 0
            out = false;
        elseif sqrt_n1<div
            out= true;
            return
        else
            div = div + 1;
            out = isDivis(n1,sqrt_n1,div);
        end
end
function lg = primeF(n1)
    if n1 == 3
        lg=true;
        return;
    end
    div = 2;
    lg = true;
    sqrt_n1 = round(sqrt(n1));
    lg = isDivis(n1,sqrt_n1,div);
end

The above picture talks about matlab code finding sum of primes within given range using Functions.

For the given input x=11 the output should be 28 (2+3+5+11+7).Hereby below attachment of output was done.

I tried my level best in uploading the scripts. Unfortunatley could not way .Please let me how to upload matlab scripts in comments , BOTH CODE EXCUTES WELL Thank you!!


Related Solutions

Write a program that prompts the user to enter two characters and display the corresponding major...
Write a program that prompts the user to enter two characters and display the corresponding major and year status. The first character indicates the major. And the second character is a number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior. We consider only the following majors: B (or b): Biology C (or c): Computer Science I (or i): Information Technology and Systems M (or m): Marketing H (or h): Healthcare Management...
Write two functions (must be titled “sumFW” and “sumFF” respectively) in MATLAB that does the following:...
Write two functions (must be titled “sumFW” and “sumFF” respectively) in MATLAB that does the following: take two integers x and y as lower limit and upper limit, then add and display the sum of all the integers between (including) the lower and upper limits. For sumFW you must use the while loop and for sumFF you must use the for loop
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they...
(Using Matlab) and "while" function 1.   Write a program that prompts the User for if they would like to enter a real number. If yes, prompt the User for the real number. Continue to do this until the User enters “no” to the first question. After the User enters “no”, display the average of all the numbers entered. (Using Matlab) and "while" function 2.   Write a program that prompts the User for if they would like to enter a real...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three...
Using MATLAB or Octave, Write a script that prompts the user for the coordinates of three points A, B, and C, namely (xA, yA), (xB, yB), (xC, yC), forming a triangle, storing each in a variable (for a total of 6 variables). The script should then calculate the centroid G = (xG, yG) using xG = xA+xB+xC 3 and yG = yA+yB+yC 3 , storing each in a variable. Finally, the script should print all four points. Sample output: Enter...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it...
Using MATLAB or Octave, Write a script that prompts the user for a multiplier, storing it in the variable fMultiplier. The script should them prompt the user for a number, storing it in the variabl fIn. The script should then calculate the product of these two variables, saving the result to another variable fOut, and printing it. The script should then repeat the prompt for fIn, calculation, and output twice more, using the same variable fIn and fOut all three...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT