Message Boards Message Boards

0
|
5480 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
POSTED BY: Paul Cleary
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