freenode/lisp - IRC Chatlog
Search
20:48:48
_death
(defun euler-1 () "Return the sum of all natural numbers below 1000 that are multiples of 3 or 5." (loop for x below 1000 when (or (multiple-of-p x 3) (multiple-of-p x 5)) sum x))
20:58:51
caltelt
The most straightforward way: (let ((threes (loop for x.....)) (fives (loop for x ...)) ...)
21:02:52
pjb
(defun times (n) (lambda (x) (* n x))) (reduce '+ '(3 5) :key (lambda (f) (reduce '+ (mapcar (times f) (iota 1000))))) #| --> 3996000 |#
21:02:53
rdap
pjb: there really is no point, it's just something that got carried over when i was trying each piece at a time
21:03:27
pjb
or even (reduce '+ '(3 5) :key (lambda (f) (reduce '+ (iota 1000) :key (times f)))) #| --> 3996000 |#
21:04:49
pjb
*** Essadon (~Essadon@81-225-32-185-no249.tbcn.telia.com) has quit: Ping timeout: 250 seconds
21:04:57
pjb
<pjb> (defun times (n) (lambda (x) (* n x))) (reduce '+ '(3 5) :key (lambda (f) (reduce '+ (mapcar (times f) (iota 1000))))) #| --> 3996000 |#
21:05:00
pjb
<rdap> pjb: there really is no point, it's just something that got carried over when i was trying each piece at a time
21:05:04
pjb
<pjb> or even (reduce '+ '(3 5) :key (lambda (f) (reduce '+ (iota 1000) :key (times f)))) #| --> 3996000 |#
21:07:33
_death
"If there is a duplication between list-1 and list-2, only one of the duplicate instances will be in the result. If either list-1 or list-2 has duplicate entries within it, the redundant entries might or might not appear in the result."
21:08:06
pjb
Yes, so from iota this should work. (reduce '+ (union (mapcar (times 3) (iota 20)) (mapcar (times 5) (iota 20)) )) #| --> 1430 |#
21:08:20
pjb
(reduce '+ (union (mapcar (times 3) (iota 1000)) (mapcar (times 5) (iota 1000)) )) #| --> 3697500 |#
21:09:43
jcowan
There's a Scheme library that provides both behaviors (treat all elements in an input list as distinct, treat elements in a list as possibly nondistinct). It would be trivial to translate it.
21:12:04
pjb
(reduce '+ (union (mapcar (times 3) (iota (truncate 1000 3))) (mapcar (times 5) (iota (truncate 1000 5))) )) #| --> 232169 |#
21:13:12
pjb
actually, I have a iota that does: (reduce '+ (union (iota 1000 0 3) (iota 1000 0 5))) #| --> 3697500 |#
21:13:49
pjb
(reduce '+ (union (iota (truncate 1000 3) 0 3) (iota (truncate 1000 5) 0 5))) #| --> 232169 |#
21:14:45
_death
(collect-sum (choose-if (disjoin (multiple-of 3) (multiple-of 5)) (scan-range :below 1000)))
21:18:19
grewal
(- (+ (loop for x from 0 below 1000 by 3 summing x) (loop for x from 0 below 1000 by 5 summing x)) (loop for x from 0 below 1000 by 15 summing x))
21:26:53
grewal
_death: (defun add-multiples-below (p1 p2 bound) "TODO: implement values for other arguments" 233168)
21:32:28
aeth
The programmatic solution to _death's problem: (defun foo () #.(- (+ (loop for x from 0 below 1000 by 3 summing x) (loop for x from 0 below 1000 by 5 summing x)) (loop for x from 0 below 1000 by 15 summing x))) (disassemble #'foo) (foo)
21:32:59
aeth
You get a function that constantly returns 233168 with no computation, but *you* don't have to compute it and hardcode it yourself!
21:56:28
Grue`
(flet ((asum (k) (let ((n (floor 1000 k))) (* k (* n (1+ n)) 1/2)))) (+ (asum 3) (asum 5) (- (asum 15))))
1:24:17
rdap
Grue`: I know... it's just something that was left from experimenting with doing it in a piecemeal fashion. The only reason it was there was so I could catch duplicates side by side when viewing the list after append. But you're right, it wasn't needed in the final solution.
2:34:28
minion
didi, memo from Grue`: you can avoid that with (funcall 'square ,y) because then you're only calling standard functions which cannot be redefined with flet, and 'square takes the global definition of square
2:36:02
didi
I was reading a paper by Constanza et al about hygienic macros in Common Lisp. Interesting stuff.
3:11:23
gilberth
There are these new innovative tablets, that don't even need any power to operate. I happen to believe they are called "paper" or something.
3:12:50
pjb
gilberth: and they even have a protocol to communicate remotely, whereby you fold the paper, write down the address (like the ip address, but more human friendly), paste a stamp, and give it to the postman. They it flies thru the postal network and is delivered to the actual mail box of the recipient!
3:13:19
pjb
And what's more, there's basically no MTU: you can seven send bigger packets without splitting them in little packets!
3:14:36
pjb
Not really. You just write your address on the back if you want to receive an answer. That's it.
3:15:36
pjb
Now the maximum ping time can count in tens of years, but it's usually around 3 or 4 days.
3:16:45
pjb
You can even use drawing as addresses: https://qz.com/770819/this-hand-drawn-map-got-a-postcard-delivered-to-a-remote-farm-in-iceland/
3:18:22
didi
gilberth: You reminded me of a quote by Tanenbaum: "Never underestimate the bandwidth of a station wagon full of tapes hurtling down the highway."
3:19:29
gilberth
didi: I was kind of citing Tanenbaum. I assumed everybody around here knew that quote.
3:20:36
pjb
If you open a letter you're not the recipient of, you can get 1 year of prison and a free of 45,000 €. (eg in France, article 225-15 du code pénal).
3:24:15
pjb
Notice the coverage: https://en.wikipedia.org/wiki/Universal_Postal_Union#/media/File:Universal_Postal_Union_membership.png
3:33:07
gilberth
Heh, my first access to e-mail was by UUCP, where ping times of a few days were common.
3:49:25
pjb
gilberth: perhaps there's already a RFC defining a gateway between the UTU postal network and the TCP/IP Internet network?
3:52:20
aeth
gilberth: Ping times of a few days? Sounds like IPoAC to me... actually IPoAC is faster than that. Really, only a few hours.
3:52:25
pjb
https://www.servicepostal.com/envoyer-lettre-en-ligne/preparer-courrier?gclid=CjwKCAjw1dzkBRBWEiwAROVDLEUdNCXDFCQk4fdoGYv0Y9d0RtrMMogAa4jvqH1CteRZDKn38Rct6hoCwsgQAvD_BwE
3:52:50
gilberth
We had something like this here once, but it was not successful. Actually I like the idea.
3:56:15
gilberth
I becomes silly, when two gateways talk to each other though. Why not send e-mail in the first place? :-)
3:58:38
gilberth
But then it is not that silly. Suppose you want to connect Mars to the internet. You better want a huge MTU.
4:05:20
gilberth
Actually, I believe the UUCP protocol would be a better fit for bringing Mars online. :)
4:09:07
no-defun-allowed
all i know is that IPFS is the only protocol to advertise itself as other-world compatible
4:09:27
pjb
https://web.archive.org/web/20110724054139/http://www.ipnsig.org/reports/IAF-Oct-2002.pdf
4:12:03
gilberth
pjb: UUCP was fun though as I had an e-mail address in something like 1991 or so as a pupil.
4:16:05
pjb
Well, we still were very happy when we got pure IP connectivity and we could do ftp directly :-)
4:25:25
rdap
and problem 2 is solved, making some use of earlier feedback: https://pastebin.com/Lx8GKFVq
7:04:45
gilberth
In the last clause: When I the number was divisible by say 5 can it still be divisible by 3?
7:07:00
loke
gilberth: Basically, it's a project that impleemnts an "enterprise" version of it. Making using of all Java Enterprise Design patterns you can think of:
7:09:32
gilberth
rdap: About the FACTOR function. IMHO it is a strange mixture of iteration and recursion.
7:12:05
loke
gilberth: It should be possible to write a method combination that selects the method based ont he divisibility, no?
7:12:32
rdap
Ah okay, the mention of 3 and 5 made me wonder if you were commenting on my solution to problem 1.
7:14:10
fiddlerwoaroof
loke: method combinations like that get into a weird interpretive place of the standard
7:14:15
loke
yes. but also with combinator APPEND, that way both fizz and buzz gets printed for arguments divisble by both5 and 3
7:14:51
gilberth
And then we need a list-of-string-concanator-singelton-class. And a protocol for that.
7:14:51
rdap
gilberth: I lifted FACTOR from here: https://rosettacode.org/wiki/Prime_decomposition#Common_Lisp
7:19:10
gilberth
rdap: Ouch. Who wrote all that code. One implementation is worse than the other, IMHO.
7:24:59
fiddlerwoaroof
gilberth: that's a common issue on that site. I mostly go there just to see what a longish piece of code in a language looks like
7:26:33
gilberth
rdap: Somebody else would pick a pure iterative version. It is a bit a matter of taste.
7:28:14
fiddlerwoaroof
If you want to avoid blowing the stack, you can simulate tail-recursion with a tagbody too.
7:28:41
fiddlerwoaroof
you just replace the recursive call with a PSETF of the arguments and a (go start)
7:29:42
fiddlerwoaroof
Well, it's more the opposite :) I knew how TCO worked, so I manually do it on occasion
7:30:05
gilberth
rdap: Start with (defun factor (n) (factor-aux n 2 (isqrt n))) (defun factor-aux (n d d-max) ...) Rest is up to you.
7:32:13
rdap
It's probably going to take a lot more reading, but thank you.. I'll see if I can make that work.
7:38:00
rdap
Thanks, I'll try to remember that, because I'm still stumped on where you're getting 3 and 5 from that
7:38:40
gilberth
Or: Would you try to divide by numbers from d-max down to 2, you would not get prime numbers.
7:45:14
Grue`
minion: memo for didi: then you won't be able to redefine square without recompiling macro and everything that uses this macro
7:47:43
Grue`
rdap: read up on Sieve of Eratosthenes, like half of project Euler problems require using it in one form or another