-2

hi there so i'm about to return to uni and so i thought id brush up on my ole matlab skills before returning so i went back to basics. at the moment im going through previous exams and the question issued was.

(b) (15 marks) Write a matlab script which counts the number of primes less than or equal to an integer n, say, where $n=2,3,...,1000$ and stores the result in an array count(n).

for e.g. count(7) would take the value 4 as there are four primes (2,3,5,7) less than or equal to 7

so heres my code (im not actually being tested so im being a bit lenient at this stage)

Matlab Code

sooo i have it accurately telling me the number of primes between 1 and n at least so far as i can tell. but it's not storing A,

now i assume (like i said, im quite rusty at this and it's taking longer than id like) that the reason why its not storing A is because im running a function and i'm missing a "store vector A for later use" command.

Further i know i could technically get around this by making it actually a script (like it asks admittedly) with a few inputs and prompts to do the same thing as a function but you know....learning and stuff.

any help would be greatly appreciated.

Vaas
  • 1,003
  • 1
    If you wanted to actually return A, then you could do that by just making that the return value. (Note by the way that right now the return value is not actually set, you should have s=numel(A) instead). But although you could postprocess the list of primes A to get what the assignment asks for, A itself is not what the assignment asks for. And in fact it would be easier to compute what the assignment asks for directly, by storing count(k) at each iteration instead of storing the primes at all. – Ian Sep 06 '17 at 19:12
  • 1
    [0 cumsum(isprime(1:n))] should do the job – N74 Sep 06 '17 at 19:18
  • 1
    @Vaas s=numel(A) instead of n=numel(A) – Max Sep 06 '17 at 19:20
  • 1
    @N74 sum(isprime(1:n)) – Max Sep 06 '17 at 19:23
  • 1
    @Max it is asked to return an array, sum would give a single value – N74 Sep 06 '17 at 19:29
  • in all honesty the ambiguityis my fault, to which i apolgise. in there was a continuation of the question which asks

    "...less than or equal to 7. Your script should also plot the values of count(n) against n as a line and, on the same graph include a plot of n/log(n)"

    Which is why i was asking for the return on the array. again my apologies to everyone, after all how can you answer a question if i dont give enough information...i'll also post the finish product for reference.

    – Vaas Sep 06 '17 at 19:45
  • I'm not entirely sure why this was closed due to lack of context. As the tag info explains, questions about technical issues (i.e. questions that aren't about mathematics) are likely more suited for SO. – Simply Beautiful Art Oct 01 '17 at 14:07

2 Answers2

1

Functions only return what you tell them to in the first line; that is, when you write function [outputs] = name(inputs) you specify what, in which order, you want to return.

Here's how I'd write this function.

function [num_primes, prime_list] = primesless(n)
    prime_list = [];
    for j = 1:n
        if isprime(j) == 1
            prime_list = [prime_list j];
        end
    end

    num_primes = len(prime_list);
end

Why this? I give variable names that make sense in what they are storing, and I give the user the option to return the number of primes, the list of primes, or both. To return just the list of primes, the user types [~, prime_list] = primesless(n) in the console. The character ~ supresses the output of the item in that position.

0

id like to thank everyone for the feedback i learnt some very useful commands, and again apologise for not posting the rest of the question... for context the entire question was

(b) (15 marks) Write a matlab script which counts the number of primes less than or equal to an integer n, say, where n=2,3,...,1000 and stores the result in an array count(n).

for e.g. count(7) would take the value 4 as there are four primes (2,3,5,7) less than or equal to 7 your script should also plot the values of count(n) against n as a line and, on the same graph include a plot of n/log(n).

(c) Add a third curve to your graph to represent $\int_2^{n} \frac{dt}{ln(t)}dt$ using matlab's integral command.

this was why i was requesting information on why A was not being stored, as i would have then used that stored vector to continue on.

in the end i just used enter image description here

which does the job

Vaas
  • 1,003