Message Boards Message Boards

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

Working with For loop results in Mathematica

Posted 9 years ago
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