0
$\begingroup$

Question:

The sequence of consecutive integers 140, 141, 142, 143, 144, 145, 146, 147, 148 consists only of composite (non-prime) numbers, and has length 9. Find the longest sequence of consecutive composite numbers in the range [1, 7919], and give the length of this sequence. Note that 7919 is a prime.

I received help previously and he suggested to try this

{Prime @@ Ordering[#, -1] + 1, Max@# - 1} &@
Differences@Prime@Range@PrimePi@7919

The problem is I'm a beginner in Mathematica, and I am having trouble understanding his input as he's more advanced. Is there another way to write this to get the longest sequence of consecutive composite integers?

$\endgroup$
  • $\begingroup$ It was from reddit, but I didn't get a response back. I thought asking stackexchange for help $\endgroup$ – user54956 Jan 28 '18 at 22:11
  • 1
    $\begingroup$ Possibly easier to understand is Max[Map[Length, Split[CompositeQ[Range[7919]]]]]. One can run it from the inside out to see what role each part plays. $\endgroup$ – Daniel Lichtblau Jan 28 '18 at 22:54
3
$\begingroup$

Find the length of the longest sequence of non primes

tmp = PrimeQ@Range[7919];
Max[Length /@ DeleteCases[Split[tmp], {True ..}]]

(* 33 *)

Alternatively

Max[Count[#, False] & /@ Split[tmp]]

find the sequence:

SequencePosition[tmp, ConstantArray[False, 33]]
(* {{1328, 1360}} *)
$\endgroup$
5
$\begingroup$

Let's try to do this at a beginner's level and leave you with at least some work to do on your own.

Knowing which numbers are prime and which are composite is a good start. To do that you want to do the same thing to every number out to 7919. "Do the same thing to every number in a list" should get you to think Map. What do you want to do? Test for primality. So

p = Map[PrimeQ, Range[7919]]

will give you a list of True and False telling you whether each number was prime or composite and that is what you want to test. You should manually check some of those and make certain you understand how PrimeQ and Map and Range worked.

Now let's see if we can verify there are 9 consecutive composites. MatchQ will test a list for a pattern and tell you True or False which means the pattern either matched or it didn't match. But you have some primes and composites before those 9 and have some primes and composites after. So you need to tell MatchQ to look for "stuff" followed by your 9 followed by "more stuff." Mathematica has pattern matching things to match "stuff." So if you try

MatchQ[p, {__, False,False,False,False,False,False,False,False,False, __}]

Notice that I used two _ at the beginning and at the end of that.Then it will return True telling you that yes it was able to find that pattern. You should look up __ in the help system and understand how that works and is different from just one _ or two or even three _ in a row.

If you make a longer and longer list of those False it will continue returning True again and again, until you get out to some length where it will return False letting you know there are not that many composites in a row. I leave it to you to figure out how many False in a row it can find.

But then the second question in your homework problem asks you to find where this pattern appears. To do that you need to know how many numbers appear before your string of False. So let's extract "stuff." You can do that with Replace by "naming" the "stuff." Notice how I name "stuff" to be x in the next line and how that differs from the previous line. By naming it x I can then do things with x, like use x again inside Replace. Replace is going to match, just like MatchQ did.

Replace[p, {x__, False,False,False,False,False,False,False,False,False, __}->{x}]

That returns 113 True and False. (How are you going to tell that there are 113? Perhaps by counting them. Or you might better use a function in Mathematica to tell you how long a list is.) That says there are 113 numbers that appear before the 9 consecutive composite numbers. If you check the numbers from 114 to 114+8 you will see they are all composite. (And how would you do that? Either by hand, or perhaps by using Map and PrimeQ and Range.)

Now you just need to do something similar with your longer list of False until you find where your longer list of composite appear.

Read the help pages for every one of the functions used here. Study those pages until you can understand why these functions did what they did. Try examples. Figure out how to use these for other things, like the homework you will probably have next week.

Note that there are always at least a dozen different ways of accomplishing anything in Mathematica and at least a couple of those are completely incomprehensible. When someone else shows you a different way of doing this then that is just another one of those dozen (or more) ways. A favorite thing for people to do is to start replacing commands like Map with cryptic punctuation characters that are abbreviations in Mathematica for actually spelling out the name of the function and using [ and ] after the name. You can learn how to do that later. Get the functions figured out first.

$\endgroup$
  • $\begingroup$ This tutorial is much appreciated. $\endgroup$ – Christopher Lamb Feb 11 '18 at 23:26

Your Answer

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