1 ! Copyright (c) 2008 Eric Mertens.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: circular disjoint-sets kernel math ranges sequences
7 ! https://projecteuler.net/problem=186
12 ! Here are the records from a busy telephone system with one
21 ! The telephone number of the caller and the called number in
22 ! record n are Caller(n) = S2n-1 and Called(n) = S2n where
23 ! S1,2,3,... come from the "Lagged Fibonacci Generator":
25 ! For 1 <= k <= 55, Sk = [100003 - 200003k + 300007k^3] (modulo
27 ! For 56 <= k, Sk = [Sk-24 + Sk-55] (modulo 1000000)
29 ! If Caller(n) = Called(n) then the user is assumed to have
30 ! misdialled and the call fails; otherwise the call is
33 ! From the start of the records, we say that any pair of users X
34 ! and Y are friends if X calls Y or vice-versa. Similarly, X is
35 ! a friend of a friend of Z if X is a friend of Y and Y is a
36 ! friend of Z; and so on for longer chains.
38 ! The Prime Minister's phone number is 524287. After how many
39 ! successful calls, not counting misdials, will 99% of the users
40 ! (including the PM) be a friend, or a friend of a friend etc.,
41 ! of the Prime Minister?
47 : (generator) ( k -- n )
48 dup sq 300007 * 200003 - * 100003 + 1000000 rem ;
50 : <generator> ( -- lag )
51 55 [1..b] [ (generator) ] map <circular> ;
54 [ [ first dup ] [ 31 swap nth ] bi + 1000000 rem ] keep circular-push ;
56 : (euler186) ( generator counter unionfind -- counter )
57 524287 over equiv-set-size 990000 < [
58 pick [ next ] [ next ] bi
62 pick equate [ 1 + ] dip
68 : <relation> ( n -- unionfind )
69 <iota> <disjoint-set> [ [ add-atom ] curry each ] keep ;
72 <generator> 0 1000000 <relation> (euler186) ;
74 ! [ euler186 ] 10 ave-time
75 ! 18572 ms ave run time - 796.87 SD (10 trials)