Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions docs/problem-120.md

This file was deleted.

43 changes: 0 additions & 43 deletions docs/problem-15.md

This file was deleted.

29 changes: 0 additions & 29 deletions docs/problem-188.md

This file was deleted.

82 changes: 0 additions & 82 deletions docs/problem-24.md

This file was deleted.

34 changes: 4 additions & 30 deletions docs/problem-26.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
title: "Problem 26: Reciprocal cycles"
title: "Problem 26: Reciprocal Cycles"
layout: post
mathjax: true
---

# Reciprocal cycles
# Reciprocal Cycles

## Problem

Here is [problem 26](https://projecteuler.net/problem=26):

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
Expand All @@ -30,6 +31,7 @@ Where $$0.1\overline{6}$$ means $$0.16666...$$, and has a 1-digit recurring cycl
Find the value of $$d \le 1000$$ for which $$\dfrac{1}{d}$$ contains the longest recurring cycle in its decimal fraction part.

## Solution

It is better to search searching for cycles starting from $$d = 999$$ and decrementing because the longest cycle will occur when $$d$$ is largest. A number $$\dfrac{1}{n}$$ cannot have more than $$n$$ repeating digits in its decimal expansion. To illustrate this, consider what is happening when you do long division to calculate $$\dfrac{1}{7}$$:

First you divide 1.0 by 7 and find the remainder. Computationally, this is the same as dividing 10 by 7, then multiplying the remainder by 10, dividing by 7 again, and so on. See the sequence of computations for calculating 1/7 below, written in this manner:
Expand Down Expand Up @@ -65,31 +67,3 @@ $$
$$

As you can see, at the next to last step, the digits start to repeat. In fact, the number of repeating digits is given by $$e = ord_{10}n$$, where $$e$$ is the smallest positive integer such that $$10^e \equiv 1 \pmod{n}$$ (this concept is called the [multiplicative order](https://en.wikipedia.org/wiki/Multiplicative_order). The preceding is not a proof of this fact, but it was enough to start searching for solutions near 999.

## Code
```racket
#!/usr/bin/env racket
#lang racket
(require (only-in rackunit check-equal?))
(require (only-in "lib/number-theory.rkt" divides? ord))


(define numbers
(filter (lambda (n) (and (not (divides? 2 n)) (not (divides? 5 n))))
(range 999 1 -1)))

(define (max-period best numbers)
(if (empty? numbers)
best
(let* ((n (first numbers))
(t (ord 10 n)))
(cond ((= t (sub1 n)) (cons n t))
((> t (cdr best)) (max-period (cons n t) (rest numbers)))
(else (max-period best (rest numbers)))))))

(define result
(car (max-period (cons 0 0) numbers)))

(displayln result)
(check-equal? result 983)
```
40 changes: 4 additions & 36 deletions docs/problem-27.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
---
title: "Problem 27: Quadratic primes"
title: "Problem 27: Quadratic Primes"
layout: post
mathjax: true
---

# Quadratic primes
# Quadratic Primes

## Problem

Here is [problem 27](https://projecteuler.net/problem=27):

Euler discovered the remarkable quadratic formula:
Expand All @@ -26,6 +27,7 @@ $$n^2+an+b$$
Find the product of the coefficients, $$a$$ and $$b$$, for the quadratic expression that produces the maximum number of primes for consecutive values of $$n$$, starting with $$n=0$$.

## Solution

We can brute force a solution by computing consecutive primes for every quadratic in the solution space of values for $$a$$ and $$b$$. However, we can narrow the search space by looking at a few constraints.

First, notice that if $$f(0)$$ isn't prime, we wouldn't have a very long prime sequence at all, so consider only values of $$b$$ that are prime.
Expand All @@ -37,37 +39,3 @@ $$f(b) = b^2+ab+b$$
$$f(b) = b(b+a+1)$$

This means that when computing consecutive primes for values of $$n$$, we only need to consider values in the range $$0 \leq n \le b$$.

## Code
With these two constraints, and a pre-computed list of primes, the following Racket solution finds $$a=-61$$ and $$b=971$$ quite quickly:

```racket
#!/usr/bin/env racket
#lang racket
(require rackunit)
(require "lib/core.rkt")
(require "lib/number-theory.rkt")


(define ps
(filter prime? (range 1000)))

(define (f n a b)
(+ (* n n) (* a n) b))

(define (primes-count a b)
(length (takef (range b) (compose prime? (curryr f a b)))))

(define m
(for*/hash ((b (append ps (map (curry * -1) ps)))
(a (range -1000 1001)))
(let ((c (primes-count a b)))
(values c (cons a b)))))

(define result
(let ((v (hash-ref m (apply max (hash-keys m)))))
(* (car v) (cdr v))))

(displayln result)
(check-equal? result -59231)
```
61 changes: 0 additions & 61 deletions docs/problem-3.md

This file was deleted.

Loading