0
$\begingroup$

I'm new to Mathematica and came across with the problem. I can't prove that (for example) 2*n is always even no matter what n is. I could do this If I had some range. (Would use Table, Mod and Length function) But I don't know what to use in this situation. And one more question please. Is it possible to have a list (1,3,5,7) and get the indexes of prime numbers (3,4) and both numbers and indexes together ({3,5}, {4, 7}). Thanks in advance.

$\endgroup$
  • $\begingroup$ 3 is also a prime number. $\endgroup$ – march Apr 13 '16 at 20:17
  • 1
    $\begingroup$ For the first, Simplify[Mod[2*n + 2, 2], n \[Element] Integers] evaluates to 0, which means that 2*n + 2 is even. Alternatively, EvenQ[2 # + 2] & /@ Range[-20, 20, 2] yields a list of True's, and you can extend the list to any maximum or minimum integer you want. $\endgroup$ – march Apr 13 '16 at 20:19
3
$\begingroup$

For the first, here are two options (the second from BlacKow):

Simplify[Mod[2*n + 2, 2], n ∈ Integers]
Simplify[Divisible[2*n + 2, 2], n \[Element] Integers]
(* 0 *)
(* True *)

means that 2*n + 2 is always even. Alternatively, checking a "few" cases:

EvenQ[2 # + 2] & /@ Range[-200, 200, 2] // And @@ # &
(* True *)

For the second question (and for future reference: asking two questions in one post that are not closely related is considered against the rules):

Sample list:

list = Range[1, 20, 2]
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

Selecting:

Select[Transpose[{Range[Length@list], list}], PrimeQ[#[[2]]] &]
(* {{2, 3}, {3, 5}, {4, 7}, {6, 11}, {7, 13}, {9, 17}, {10, 19}} *)
$\endgroup$
  • 1
    $\begingroup$ or you can use Divisible instead of Mod $\endgroup$ – BlacKow Apr 13 '16 at 20:34
  • $\begingroup$ @BlacKow. Oh yeah, that's better! $\endgroup$ – march Apr 13 '16 at 20:35
  • $\begingroup$ is 2n not a more complete and simpler test that 2n+2? $\endgroup$ – RunnyKine Apr 13 '16 at 21:42
  • $\begingroup$ FullSimplify[Mod[2 n, 2] == 0, n \[Element] Integers] returns True $\endgroup$ – bill s Apr 13 '16 at 22:48
1
$\begingroup$

Another method for the second question uses the Position function along with the _? (PatternTest) construct:

list = Range[1, 20, 2]
Transpose[{Flatten[Position[list, _?PrimeQ]], Select[list, PrimeQ]}]
(* {{2, 3}, {3, 5}, {4, 7}, {6, 11}, {7, 13}, {9, 17}, {10, 19}} *)
$\endgroup$
  • $\begingroup$ Aren't you testing for being prime twice? What about Transpose@{#, list[[#]]} &@Flatten[Position[list, _?PrimeQ]] $\endgroup$ – BlacKow Apr 13 '16 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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