%% question 1
% This program finds first 1000 prime numbers and store in the
array
% arr_of_primes
clc
clear all
arr_of_primes = []; % Array of prime numbers
N = 2; % starting with N = 2
while length(arr_of_primes)<1000 % while no of elements in P is
less than 1000
logic = ifPrime(N); % check if N is prime number
% logic = 1, if N is prime number
% logic = 0, if N is not a prime number
if(logic==1) % if N is prime number, store it in array P
arr_of_primes = [arr_of_primes, N];
end
N = N +1; % check next number
end
%% fuction file
function logic = ifPrime(N)
logic = 1; % assume N is prime number
% 2 is a prime nuber, no need to check
% if N is not 2, check if it is not a prime number
if(N~=2)
% first devide by 2, if remender is zero,it is not a prime no
% otherwise apply next check
r = mod(N,2); % remender when devide N by 2
q = floor(N/2); % quotent when devide N by 2
if(r==0) % if remender when devide by 2 is zero, it is not a prime
no
logic = 0;
else if(N>3) % Else if number is greater than 2, do fuether
check
% start deviding with 3
div = 3; % div is divisor variable
while div < q
r = mod(N,div);
q = floor(N/div);
if(r==0)
logic = 0;
break
else
div = div + 2;
end
end
end
end
end
end
/////////////////////////////////////////////////////////////////////////////// (2)
function [ arr_des, arr_asc ] = sort_descending_ascending( arr
)
% This function arrange input array into in ascending and
descending order
% Only non-negative integer should be input to this function
% assign first element of arr to variables arr_des and
arr_asc
arr_des = arr(1);
arr_asc = arr(1);
% Arranging in accending and descending order one-by-one
element
for k = 2:length(arr)
% descending order
if(arr(k)<=arr_des(k-1)) % if upcoming element is greater than
last element in array arr_des
arr_des = [ arr_des, arr(k)];
else % otherwise, find the right postion in array arr_des and put
it at right place
j = 1;
while arr_des(j)>arr(k)
j = j + 1;
end
arr_des = [arr_des(1:j-1), arr(k), arr_des(j:end)];
end
% ascending order
if(arr(k)>=arr_asc(k-1)) % if upcoming element is less than last
element in array arr_asc
arr_asc = [ arr_asc, arr(k)];
else % otherwise, find the right postion in array arr_des and put
it at right place
j = 1;
while arr_asc(j)<arr(k)
j = j + 1;
end
arr_asc = [arr_asc(1:j-1), arr(k), arr_asc(j:end)];
end
end
end
write a Matlab program ( solve the three questions). please use array and create your own...
use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...
Using MatLab Write a function called PrimeFinder that receives an integer n as an input argument, and provides an output argument called Primes that is a vector containing all prime numbers between 2 and n, inclusive. Verify the correctness of your code with inserting 13 as the input argument, and check that your output is Primes = [2,3,5,7,11,13]. You are NOT allowed to use any directly relevant MATLAB built-in function such as divisors, etc. NOTE: A prime number is a...
MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a while-loop to find and return k, the smallest prime number that is greater than n. Feel free to use the built-in isprime function (do not use nextprime). MATLAB
21. An isolated prime is a prime number p such that neither p -2 nor p+2 is prime. For example, 47 is an isolated prime since 45 and 49 are both not primes. Write a computer program that finds all the isolated primes between 50 and 100. Do not use MATLAB's built-in function isprime. Matlab Instructions: Make in while and for, mode 21. An isolated prime is a prime number p such that neither p -2 nor p+2 is prime....
#include <assert.h> #include <stdio.h> #include <stdlib.h> // initialize_array is given an array "arr" of "n" elements. // It initializes the array by setting all elements to be "true" (any non-zero value). void initialize_array(int *arr, int n) { // TODO: Your code here. assert(0); } // mark_multiples is given an array "arr" of size n and a (prime) number "p" less than "n" // It assigns "false" (the zero value) to elements at array indexes 2*p, 3*p, 4*p,.., x*p (where x*p...
Use MATLAB to program the following: 9. The MATLAB built-in function primes (x) finds all the prime numbers less than x. Write a M-file function "addupprime" that will sum up all the prime numbers of an input number x. Run and show 2 test cases to demonstrate that your function file is correct.
3. Primes primes.py We're going to solve a popular interview question! Write a function that checks whether a number is prime. Handle the case where the argument is not a positive integer. Next, write a function that returns the first N primes. Can you optimize your code? Note that any number that is not prime is always divisible by at least one prime that comes before it. Ensure your program passes the tests provided.
Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...
this program is in C. Write a program that computes the number of elements in an array divisible by a user specified number. Declare an integer array of size 7 and read the array elements from the user. Then, read a number k from the user and compute the number of elements in the array divisible by k. Consider the following example. 3 elements in this array are divisible by 2 ({2,2,4}). Sample execution of the program for this array...
1. Write a function in matlab platform for each part as described. a. Obtains the multiplication of elements of n-sized vector, where n is a positive integer. Name of this function must be “all2”. b. Obtains the sum of elements of nxn-sized matrix, where n is a positive integer. Name of this function must be “will3”. c. Obtains the sum of prime factors of an integer. Name of this function must be “be0”. Example (1): Prime factors of 84 are...