Message Boards Message Boards

0
|
4986 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Working with For loop results in Mathematica

Posted 9 years ago

I am currently looking for a way to identify a palindromic sequence (same forwards and backwards) from a long string of numbers. This is the sequence I've been using:

sequence={6,3,2,4,**1,2,3,4,5,4,3,2,1**,3}

Note that positions 5 through 13 are the palindrome; 1,2,3,4,5,4,3,2,1. The way I have it set up, I am looking for palindromes of length 9 (query length; ql), such as this one, and the loop length is half of the palindrome length (loop length; LL), excluding the spacer which is number 5 in this case.

sl=Length[sequence] = 14

ql= 9 (I happen to be looking for palindromes of length 9 in this case)

LL=Round[ql/2-0.1] = 4

*note: I capitalized LL for increased visibility here in this post, I use lowercase letters for actual the actual inputs.

Recently, using a For loop function I have been able to list out every frame of 4 numbers in a row, starting with the first four, then on to the second four, and so on until I list the final four. Here is what I mean:

sequence={6,3,2,4,1,2,3,4,5,4,3,2,1,3}
input: For[n=1.n<=sl-LL-1,n++,Print[Part[sequence,n;;n+LL-1]]]

output:

{6,3,2,4}

{3,2,4,1}

{2,4,1,2}

{4,1,2,3}

{1,2,3,4}

{2,3,4,5}

{3,4,5,4}

{4,5,4,3}

{5,4,3,2}

{4,3,2,1}

{3,2,1,3}

Now here is my question:

Is there any way I can compare results specifically for palindromes of length 9? For example, we can see that the 5th result {1,2,3,4} is equivalent to the 10th result {4,3,2,1} if you were to reverse it. What I'm looking for is a way I can input the entire 14 number-long sequence, and have the output give a table or a matrix of some sort that shows when sequences of four match up. Another acceptable way would be some order of operations that would allow me to perform further functions on the results of the For loop (For loop within a For loop?).

I suspect functions such as NestWhile, Nest, Do, and If may be useful, but am struggling to find the appropriate combination of functions that will assist me. Also I suspect it might be more useful to list out results of length 9 (the query length) instead of length 4, but I still run into the problem of being able to further perform operations on the individual results afterwards.

Also, if anyone has any hints or advice on matrix math, or matrix functions that can help me, please do share! I have yet to learn basic matrix operations, but learning them is on my agenda for the near future.

Thank you for any and all help!

POSTED BY: Erik Gentzel
3 Replies

I might have misunderstood what you are going for, but I would not use any loops. Here is a list of $10^5$ integers.

data = RandomInteger[9, 10^5];

Sliding window of segments of 9 elements:

nines = Partition[data, 9, 1];

You got 12 9-palindromes out of $10^5$ integers:

{
 {2, 0, 7, 5, 0, 5, 7, 0, 2},
 {6, 1, 5, 0, 0, 0, 5, 1, 6},
 {9, 4, 4, 1, 6, 1, 4, 4, 9},
 {9, 4, 7, 5, 1, 5, 7, 4, 9},
 {0, 6, 9, 0, 8, 0, 9, 6, 0},
 {6, 1, 4, 7, 5, 7, 4, 1, 6},
 {3, 0, 6, 0, 0, 0, 6, 0, 3},
 {4, 9, 4, 6, 6, 6, 4, 9, 4},
 {7, 5, 9, 5, 6, 5, 9, 5, 7},
 {0, 5, 7, 7, 7, 7, 7, 5, 0},
 {9, 0, 1, 4, 0, 4, 1, 0, 9},
 {4, 2, 0, 5, 1, 5, 0, 2, 4}
}

This is the index at what number polindromes start:

index = Flatten[Position[nines, #] & /@ polys]

{8720, 9304, 19630, 23868, 33381, 34247, 48155, 53876, 72100, 76200, 81983, 89946}

And this is the way to check it:

data[[# ;; # + 9]] & /@ index
POSTED BY: Vitaliy Kaurov
Posted 9 years ago

Is this something like what you need?

sequence = {6, 3, 2, 4, 1, 2, 3, 4, 5, 4, 3, 2, 1, 3}; Do[
 s = sequence[[a ;; b]]; 
 If[s == Reverse[s], Print[s]], {a, 1, Length[sequence] - 1}, {b, 
  a + 1, Length[sequence]}]


{1,2,3,4,5,4,3,2,1}

{2,3,4,5,4,3,2}

{3,4,5,4,3}

{4,5,4}
POSTED BY: Paul Cleary

Here is one way of approaching this I think would be effective:

If I list out results of length 9, I obtain the following:

{6,3,2,4,1,2,3,4,5}

{3,2,4,1,2,3,4,5,4}

{2,4,1,2,3,4,5,4,3}

{4,1,2,3,4,5,4,3,2}

{1,2,3,4,5,4,3,2,1}

{2,3,4,5,4,3,2,1,3}

Is there any way I could write the For loop function (For loop within a For loop) such that I look at each result, and query if position 1 is equal to position 9 and if 2 is equal to 8 and so on; such as:

Table[If[Part[result,n]==Part[result,ql+1-n],true,false,{n,1,LL}]  

and if I obtain any falses, I discard the result?

This would discard the first four results as they are not palindromes, but it would list out the 5th result, {1,2,3,4,5,4,3,2,1}, and would also discard the 6th result.

POSTED BY: Erik Gentzel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract