If you map out the power of your computer year by year from a base line at say 1985

And then subtract out the fraction of this power that your computer expends on background tasks that you didn't ask for, don't understand and can do nothing about

Then you get a pretty good model of how much you can actually get done

So it is not my imagination, the nineties really were the Golden Age.

## Thursday, 16 July 2015

## Wednesday, 15 July 2015

### Problem of Nines, alternative method

Well, there is a quick way I find on Stack Overflow to convert an integer to the binary string equivalent - import

Prelude> import Text.Printf

Prelude Text.Printf> let nines = [ 9 * read (printf "%b" n) | n <- [1..] ]

Test this:

Prelude Text.Printf> take 10 nines

[9,90,99,900,909,990,999,9000,9009,9090]

So the question asked for the first of these numbers that is a multiple of 23:

Prelude Text.Printf> head [ n | n <- nines, n `mod` 23 == 0 ]

990909

I like the other way better partly because I'm not sure how this function

**Text.Printf**and then use the**printf**function with**"%b"**as a descriptor. If this doesn't work then you might need a newer version of Haskell. Anyway,**read**this to convert to an integer and then multiply by 9 - so the list of numbers that are all made of digits 0 and 9 can be defined so:Prelude> import Text.Printf

Prelude Text.Printf> let nines = [ 9 * read (printf "%b" n) | n <- [1..] ]

Test this:

Prelude Text.Printf> take 10 nines

[9,90,99,900,909,990,999,9000,9009,9090]

So the question asked for the first of these numbers that is a multiple of 23:

Prelude Text.Printf> head [ n | n <- nines, n `mod` 23 == 0 ]

990909

I like the other way better partly because I'm not sure how this function

**printf**works. Doesn't it have to take a variable number of parameters to match the placeholders in the string? How does that work against the type checker? Some reading to do## Tuesday, 14 July 2015

### Problem of Nines

Here is a pretty problem from Programming Praxis that has popped up on Planet Scheme... what is the lowest number that is a multiple of 23 that is made of only the digits 0 and 9?

One way to find it would be to scan through all the numbers made of 0s and 9s. This looks tricky until you see the numbers:

9

90

99

900

909

990

999

...

Aha, you say, these are just like binary numbers:

1

10

11

100

101

110

111

Except take each one and multiply by 9 and Bob's your uncle you have all the numbers made of 0s and 9s. Scan through and pick the first one that is a multiple of 23. I'll try that approach later when I've figured out how you convert a number to a binary digit sequence. Must be a simple way.

However the alternative path to the solution is to produce the list of multiples of 23 and pick the first one that is made only of 0s and 9s.

Now the multiples of 23 are easy - it's just

Prelude> take 10 [23,46..]

[23,46,69,92,115,138,161,184,207,230]

One way to find it would be to scan through all the numbers made of 0s and 9s. This looks tricky until you see the numbers:

9

90

99

900

909

990

999

...

Aha, you say, these are just like binary numbers:

1

10

11

100

101

110

111

Except take each one and multiply by 9 and Bob's your uncle you have all the numbers made of 0s and 9s. Scan through and pick the first one that is a multiple of 23. I'll try that approach later when I've figured out how you convert a number to a binary digit sequence. Must be a simple way.

However the alternative path to the solution is to produce the list of multiples of 23 and pick the first one that is made only of 0s and 9s.

Now the multiples of 23 are easy - it's just

**[23,46..]**:Prelude> take 10 [23,46..]

[23,46,69,92,115,138,161,184,207,230]

And the condition that the number is made of 0s and 9s? - well, given a number n we say

**read n**to convert to a string, which is just a list of characters: we can then use the function**all**to apply the test to this list that all the characters are either 9 or 0, that is to say all are an element of the list**"90"**- we use the function**elem**for this:
Prelude> all (`elem` "09") "9080"

False

Prelude> all (`elem` "09") "9090"

True

So we set up a list of all numbers

**n**drawn from the multiples [23,46..] subject to the condition that all the characters of the string representation of**n**are in the list "90" - in other words
[ n | n <- [23,46..], all (`elem` "90") (show n) ]

Of course this is an infinite list, but we just ask for the

**first**element, i.e. the**head**, as follows:
Prelude> head [ n | n <- [23,46..], all (`elem` "90") (show n) ]

990909

So the answer is 990909. In fact let's see the first 10 solutions:

Prelude> take 10 [ n | n <- [23,46..], all (\c -> c=='0' || c=='9') (show n) ]

[990909,9099099,9909090,90000909,90990990,99090900,99909999,99999009Interrupted.

An infinite list is like the Tao, it seems empty, but draw upon it and it is never exhausted - - although in this case I gave up waiting after the eighth answer...

Subscribe to:
Posts (Atom)