Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 how do you recognize a pattern in a set of array?
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Horus




PostPosted: Mon Feb 16, 2009 11:50 am   Post subject: how do you recognize a pattern in a set of array?

for example if your array is 1,2,1,2,1,2,1,2, then the program can predict that the next number is 1.

if your array is 1,6,3,1,6,3,1,6 the program can predict that the next number is 3

what's the most effective algorithm to do this?

I'm thinking of using a for loop and compare the first 2 set of the array with the next 2 set, and then compare the first 3 set with the next 3 set and then compare the first 4 set with the next 4 set and etc...

is there a better way to do this?
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Mon Feb 16, 2009 12:07 pm   Post subject: Re: how do you recognize a pattern in a set of array?

I say that the best way to do this is by first finding what the pattern is.....for example, if you know that your pattern contains of unique integers (i.e. it doesn't have any repetitions), then all you have to do is go through your array until you reach an element that has been used before. For example, in the following array : 1 4 2 5 1 4 ..... , when the you reach the second 1, then you know that the pattern is going to repeat.

Now, after finding the pattern, then it is quite simple. If you want to know what the 10th element is in the above array that I gave as an example, you can do the following :

1) You know that the pattern repeats every 4 elements (i.e. the length of the pattern is 4)
2) All you have to do now is divide 10 by 4, and see what remainder you get. If you get a remainder of 1, then you know that the element is the same as the first element of the pattern. If the remainder is 2, then the element is the same as the 2nd element in the pattern, and so on....but if you get a remainder of 0, then the element will be the same as the 4th element in your pattern.

Here, since 10 gives a remainder of 2 when divided by 4, the 10th element in the above array will be the same as the second element of the pattern (i.e. '4').

Using these remainders you get when dividing 'x' and 'n' (where 'n' is the number of elements in your pattern, and 'x' is the term you want from the array), tells you what the 'x'th element is......


I hope this helped Wink
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: