]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/148/148.factor
project-euler: Rewrap, update links, add copyrights, tests
[factor.git] / extra / project-euler / 148 / 148.factor
1 ! Copyright (c) 2008 Eric Mertens.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions sequences project-euler.common
4 ;
5 IN: project-euler.148
6
7 ! https://projecteuler.net/problem=148
8
9 ! DESCRIPTION
10 ! -----------
11
12 ! We can easily verify that none of the entries in the first
13 ! seven rows of Pascal's triangle are divisible by 7:
14
15 !                             1
16 !                         1       1
17 !                     1       2       1
18 !                 1       3       3       1
19 !             1       4       6       4       1
20 !         1       5      10      10       5       1
21 !    1        6      15      20      15       6       1
22
23 ! However, if we check the first one hundred rows, we will find
24 ! that only 2361 of the 5050 entries are not divisible by 7.
25
26 ! Find the number of entries which are not divisible by 7 in the
27 ! first one billion (10^9) rows of Pascal's triangle.
28
29
30 ! SOLUTION
31 ! --------
32
33 <PRIVATE
34
35 : sum-1toN ( n -- sum )
36     dup 1 + * 2/ ; inline
37
38 : >base7 ( x -- y )
39     [ dup 0 > ] [ 7 /mod ] produce nip ;
40
41 : (use-digit) ( prev x index -- next )
42     [ [ 1 + * ] [ sum-1toN 7 sum-1toN ] bi ] dip ^ * + ;
43
44 : (euler148) ( x -- y )
45     >base7 0 [ (use-digit) ] reduce-index ;
46
47 PRIVATE>
48
49 : euler148 ( -- answer )
50     10 9 ^ (euler148) ;
51
52 ! [ euler148 ] 100 ave-time
53 ! 0 ms ave run time - 0.17 SD (100 trials)
54
55 SOLUTION: euler148