# check if there are consecutive numbers in list

I have a list like the following:

- nmbList={4,-2,15,3,5,56,100}

and I would like to loop through that list and check if there are consecutive numbers in the list (in above list, there are three consecutive numbers: 3,4 and 5) and, if are, to automatically count how many...

Is this possible with only GeoGebra scripting?

I've created the JS function for that:

- function pronadjiUzastopne(array) {
- var nmbOfSeq = 0;
- for(var i=0; i<array.length; i++) {
- for(var j=0; j<array.length; j++) {
- if(array[j]==array[i]+1) {
- nmbOfSeq+=1;
- }
- }
- }
- return nmbOfSeq;
- }

in which the returning value of the function (nmbOfSeq) is one less than the total number of consecutive numbers in the sequence because it's increased by one for each pair of consecutive numbers in the sequence...

Now, I'm working with students who are not as skilled in programming so I wonder if there are some commands by which I could come with the same result..?

Thanks in advance for your help,

Aleksandra-Maria Vuković

Hi, try :

Hi, try :

Works fantastic!

Thank you very much - now I have to study the GeoGebra syntax and commands you've used in the solution... I see the command Join[] that I've previously used only in the basic usage... Once again, I appreciate your help and quick response!

Also CountIf and Zip are useful when dealing with lists

Assuming unique numbers this might work:

I came upon another problem based on the error that I had in my original JS code... The var nmbOfSeq is increased by 1 each time the pair of consecutive numbers occur which means that if there are, for example, numbers 1,5,6,9,10,15 in array the output will also be 2 (the value of the nmbOfSeq) as well as if there are numbers 1,5,6,7,10,15 in the array!

As I'm creating the Three in a row game I need to find the exact sequence of three consecutive numbers, so I've came upon this solution based on the original Michel Iroir's solution:

and it works perfect! The output value is 1 in this case (if there are three consecutive numbers) or, in general, the output value is 2 less than the number of consecutive numbers in array...

Now I'm going to try Zbynek's solution!

Once again thanks both of you - you're really helped me a lot!

Zbynek, your solution really rocks except when having two pairs of consecutive numbers which don't form a sequence of consecutive numbers... This is due to my original error in JS code on which you based your solution...

Meaning, if there is numList = {2, 3, 6, 7, 12} the output will be 2 which is fine, but perhaps I didn't give enough detailed explanation at the beginning that I need to count only the sequences of 3 and more consecutive numbers... but never the pairs... Because I want to detect if three (in the game I'm developing now) points are collinear vertically, horizontally or diagonally and if their corresponding coordinates are consecutive (if so, I have three in a row...)

I need more complex procedure and I can not just use the AreCollinear[] command because I don't know in advance the indexes of the points, whose coordinates are stored in a list, which are potentially collinear nor their coordinates...

Anyway, once again - great and neat solution and a good idea which I will sooner or later use in some other code!

Loading...Comments have been locked on this page!