How can I solve this problem?

3 Ansichten (letzte 30 Tage)
David Lopez
David Lopez am 21 Sep. 2016
Beantwortet: RAMAKANT SHAKYA am 7 Feb. 2019
I am a bit lost with this Matlab problema that I have to do. Can anyone give me a hand? I would be very grateful!
" Write a function called circular_primes that finds the number of circular prime numbers smaller than n, where n is a positive integer scalar input argument. For example, the number, 197, is a circular prime because all rotations of its digits: 197, 971, and 719, are themselves prime. For instance, there are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. It is important to emphasize that rotation means circular permutation not all possible permutations "
"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. Write a function called smallest_multiple that returns a uint64, the smallest positive number that is evenly divisible by all of the numbers from 1 to n where n is a positive integer scalar and is the only input argument of the function. If the result would be greater than what can be represented as a uint64, the function returns 0 "

Antworten (2)

Srishti Saha
Srishti Saha am 11 Mär. 2018
This should work perfectly:
function mul = smallest_multiple(n)
facts = zeros(1,n); % store the exponents of various factors
for ii = 2:n
f = factor(ii); % get factors for current integer
for jj = 2:ii
k = sum(f == jj); % what's the exponent of this factor?
if k > facts(jj) % if it is greater than what we have so far
facts(jj) = k; % update to this new value
end
end
end
% Compute the result with one command.
% The 'native' option tells MATLAB to work in uint64
mul = prod(uint64((1:n).^facts),'native');
if mul == intmax('uint64')
mul = uint64(0);
end
end

RAMAKANT SHAKYA
RAMAKANT SHAKYA am 7 Feb. 2019
function out=circular_primes(no)
prim=primes(no);% find the all prime number till the given number
pr=0;
nos=[];
po=[];
for p=1:length(prim)
n=prim(p); % picking up each prime no one by one
n=num2str(n);% change into string for rotation of no
d=length(n); % length of string
if d>1 % take nos greater than 10 beacuase below 10 no need for rotation
for h=1:d
a=n(1);
for r=1:d % for rotation right to left
if r==d 5 % for the last element of string
n(d)=a;
else
n(r)=n(r+1); %shifting
end
end
s=str2num(n); % string to number
nos=[nos,s]; % store rotated elements in array
end
if nos(end)==no %if given no is also a circular prime we need smaller
break;
end
for gr=1:length(nos) % checking rotated nos are prime or not
p1=isprime(nos(gr));
po=[po,p1]; %storing logical result in array
end
if sum(po(:))==length(nos) %if all are prime the length and sum are must be equal
pr=pr+1;
out=pr;
else
out=pr;
end
po=[];
nos=[];
else
s=str2num(n); %numbers less than 10
f=isprime(s);
if f==1
pr=pr+1;
out=pr;
else
out=pr;
end
end
end
end

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by