0
$\begingroup$
    f =  9x^2 - 78x-10000
    list1 = PrimeQ[Abs[f /. x -> {Range[1, 20]}]]
    Count[list1,True]

This is my code. Im trying to find how many prime numbers there are between the range 1-20. If a number is negative i have to take the absolute value of it.

I need to know how many numbers are prime. I tried using count(i want to see how many times true shows up) but it keeps returning 0 thank you

$\endgroup$
9
  • 1
    $\begingroup$ Your code can be simplify. f = 9 x^2 - 78 x - 10000; list1 = PrimeQ[Abs[f /. x -> Range[1, 20]]]; Count[list1, True] $\endgroup$
    – cvgmt
    Sep 28, 2020 at 14:26
  • $\begingroup$ how can i add simplify? thank you $\endgroup$
    – Aran
    Sep 28, 2020 at 14:33
  • $\begingroup$ thank you i just tried it it works! $\endgroup$
    – Aran
    Sep 28, 2020 at 14:43
  • $\begingroup$ Abs is not need. $\endgroup$
    – cvgmt
    Sep 28, 2020 at 14:43
  • $\begingroup$ My prof said to take the absolute value of the negative numbers, what else can i use besides abs, thank you $\endgroup$
    – Aran
    Sep 28, 2020 at 14:45

1 Answer 1

0
$\begingroup$

Seems a pity not to see the values of the function that are prime as well as how many there are. We can avoid suppressing them while still getting the answer we want:

f = 9 x^2 - 78 x - 10000
primes = Select[f /.x -> Range[1, 20], PrimeQ]
Length[primes]

PrimeQ automatically treats negative primes as prime, which is why you don't need Abs.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.