Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Consider the following code to produce the sequence $x_1,\ldots,x_{n+1}$ where $x_i=11\cdots1$ ($i$ digits 1). Is there an easier way to do this?

n = 7
X = Table[Sum[10^i, {i, 0, k - 1}], {k, 1, n + 1}]
R = Table[Mod[X[[k]], n], {k, 1, n + 1}]
X 
R

Also, the code defines the list R of remainders on division of $x_i$ by $n$. The output of R is {1, 4, 6, 5, 2, 0, 1, 4}.

I'd like to do the following: determine the indexes producing the first two equal elements of R (for example, since R[[1]]=R[[7]] I'd like to do some math with X[[1]] and X[[7]]).

share|improve this question
    
Why are you using SetDelayed[]? –  belisarius May 31 at 0:08
    
@belisarius, sorry. I have no idea what you are talking. –  Sigur May 31 at 0:09
    
Why are you using := instead of =? –  belisarius May 31 at 0:09
    
Is it not the way to define something?! To store on a variable. –  Sigur May 31 at 0:10
1  
Then read this Q&A mathematica.stackexchange.com/questions/18393/… –  belisarius May 31 at 0:11

2 Answers 2

up vote 2 down vote accepted
r = {1, 4, 6, 5, 2, 0, 1, 4};

sol = Select[GatherBy[Range[Length@r], r[[#]] &], Length@# > 1 &, 1][[1,;;2]]
(* {1,7} *)

(Credit: @Szabolcs's answer in this Q/A)

or

sol2 = ## & @@@ Position[r, (Select[Gather[r], Length@# > 1 &, 1][[1, 1]]), 1, 2]

or

sol3 = ReplaceList[r, {a___, b : PatternSequence[i_, ___, i_], ___} :> 
                       Sequence[1 + Length[{a}], Length[{a}] + Length[{b}]], 1]

Update:

f[n_] := Module[{x = Table[Sum[10^i, {i, 0, k - 1}], {k, 1, n + 1}],  r, sol},
       r = Mod[x, n]; 
       sol =Select[GatherBy[Range[Length@r], r[[#]] &], Length@# > 1 &, 1][[1,;;2]]; 
       {x, r, sol, x[[sol[[2]]]] - x[[sol[[1]]]]}]

f[7]
(* {{1,11,111,1111,11111,111111,1111111,11111111},
    {1,4,6,5,2,0,1,4},
    {1,7},
    1111110} *)
share|improve this answer
    
Very short code. Is it possible to save the result on sol for example, so I could use simply sol[[1]] to make use of 1? –  Sigur May 31 at 0:21
1  
@Sigur, with the updated version sol[[1]] gives 1 instead of {1}. –  kguler May 31 at 0:24
    
Please, could you try with n=2? I'm getting error Part specification 1[[-1]] is longer than depth of object. >>. –  Sigur May 31 at 0:30
    
Yes, perfect. Thanks so much. With your help I can illustrate the fact that any natural $n$ has a multiple written using only 1 or 0. –  Sigur May 31 at 0:48
1  
@Sigur, pls see the update. –  kguler May 31 at 2:43
n = 7;

{X, R} = Table[(10^x - 1)/9, {x, 1, n+1}] // {#, Mod[#, n]} &;

X

R

matchPairs = Subsets[Range@Length@R, {2}][[First@Position[Subsets[R, {2}], {x_, x_}]]]

(*

{1, 11, 111, 1111, 11111, 111111, 1111111, 11111111}
{1, 4, 6, 5, 2,0, 1, 4}

{{1,7}}

*)

Just take First@matchPairs to get first only of any pairs of indices that have same value for remainder.

BTW - bad idea to use uppercase symbols/initials - you can clash with built-ins...

share|improve this answer
    
Nice! In this case, to use the number 1 from the output {{1,7}} I have to use matchPairs[[1,1]], right? Is it possible to save so that I simply use it as matchPairs[[1]]? –  Sigur May 31 at 0:19
    
@Sigur: As I said, just add First@ in front of the Subsets..., and you'll get the first match only, whereby you can address them as desired. –  rasher May 31 at 0:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.