Question

I ran some test code in octave and found out that isprime doesn't seem to work with decimals, does anyone have a work around. I'm trying to split an array up into primes and not primes and the array has decimals and integers in it. See the example code below. The error message is isprime: needs positive integers

clear all,clf,tic
%a=(1:25)'
a=(1:0.5:25)';
b=[a,a.^2];


cprime=[];
cnoprime=[];
for ii=1:1:length(b)
    if isprime(b(ii,1))
        cprime=[cprime;(b(ii,:))];
    elseif
        cnoprime=[cnoprime;(b(ii,:))];
    endif
end

Thanks to Jerad for getting me this far

Was it helpful?

Solution

You can use this function:

floatisprime@(a)(round(a)==a)&isprime(round(a))

Logic seems a bit strange, but it's a prime if the value is integer (round(a)==a) and the next integer is a prime (isprime(round(a))

If you use the setdiff-idea from the previous question, you don't have the issue:

noprimes=setdiff(a,primes(max(a)))

then iterate over noprimes

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top