Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-8081-

Divisible by 3 or 5, in lisp

Name: Anonymous 2007-08-12 17:20 ID:RyfBwU/E

(lambda (n) (or (= 0 (mod n 3)) (= 0 (mod n 5))))

how on earth do you write this in lisp, I mean better than I have here.. surely it must be possible?

Rube Goldberg devices are definitely acceptable if it makes the final code better.. hope you have some ideas /prog/

Name: Anonymous 2007-08-12 17:41 ID:Mvf+qhCr

(lambda (n)
  (let ((divisible (lambda (k) (= 0 (mod n k)))))
    (or (divisible 3) (divisible 5))))

Name: Anonymous 2007-08-12 17:44 ID:Mvf+qhCr

(define (divisible n k) (= 0 (mod n k)))
(define (divisible-by-list n l) (fold (lambda (k o) (or o (divisible n k))) #f l))

Name: Anonymous 2007-08-12 17:47 ID:Mvf+qhCr

(define (divisible n l)
 (if (pair? l) #f
     (if (= (mod n (car l)) 0) #t
         (divisible n (cdr l)))))

(divisible n '(3 5))

Name: Anonymous 2007-08-12 18:05 ID:RyfBwU/E

>>3
ok this is neat
I think I want to generalize though

divisible = (lambda (n d) (= 0 (mod n d))
f-by-list = (lambda (n l) (fold (lambda (k o) (or o (f n k))) #f 1))
divisble-by-list = (lambda (n l) (fold (lambda (k o) (or o ((lambda (n d) (= 0 (mod n d)) n k))) #f 1))

((lambda (n l) (fold (lambda (k o) (or o ((lambda (n d) (= 0 (mod n d)) n k))) #f 1)) n '(3 5))

hmm

Name: Anonymous 2007-08-12 18:08 ID:UuB0Fh1x

LOL Lisp.
int f(n){(!(n%3) || !(n%5))}

Name: Anonymous 2007-08-12 18:19 ID:+1As6lo3

LOL De Morgan.
int f(n){!(n%3 && n%5)}

Name: Anonymous 2007-08-12 18:23 ID:UuB0Fh1x

>>7
LOL some sort of newfag college undergrad who just finished discrete math. My version is clearly more readable, hence it is superior to yours in every way possible.

Name: Anonymous 2007-08-12 18:38 ID:+1As6lo3

LOL hurt pride

Really, just use haskell for euler, you'll finish the first 50 or so in no time.

Name: Anonymous 2007-08-12 18:48 ID:ZL/ORnBu

>>6,7,8
you both forgot return. fail.

Name: Anonymous 2007-08-12 21:52 ID:RyfBwU/E

>>7
nice, well done

Name: Anonymous 2007-08-12 21:52 ID:RyfBwU/E

>>10
C faggot

Name: Anonymous 2007-08-12 22:04 ID:RyfBwU/E

(not (/= 0 (mod n 3) (mod n 5)))

Name: Anonymous 2007-08-12 22:04 ID:Heaven

>>13
FAIL

Name: Anonymous 2007-08-12 23:42 ID:dvYoKBJB

>>6
no shit dude. what you're doing is essentially
(define (f n) (or (not (divisible n 3)) (not (divisible n 5))))

but you use builtin syntax, that's why it looks shorter.

(spoiler: builtin syntax for that stuff is only useful in the rare cases you need to write silly code examples for a prog forum)

Name: Anonymous 2007-08-12 23:53 ID:RyfBwU/E

MOAN COMPLAIN HOW DO I DO IT BETTER IN LISP THO?

Name: Anonymous 2007-08-13 0:10 ID:WEd4ro21

Name: Anonymous 2007-08-13 0:18 ID:WEd4ro21

>>16
>>4
or something like
(defun f (n l) (find-if (p/ divisible n) l)
if you use the ugly p/ macro I made.
so you call it like this:

(f 1 '(3 5))
or
(f n '(3 5 7 11))
or any list you want. the exact example you asked for:

(lambda (n) (find-if (p/ divisible n) '(3 5)))
without the macro, in plain lisp
(lambda (n) (find-if (lambda (k) (divisible n k)) '(3 5)))

Name: Anonymous 2007-08-13 0:45 ID:3J35KuIg

>>18
WTF.. find-if wont work

Name: Anonymous 2007-08-13 1:04 ID:3J35KuIg

>>18
did you mean every?

Name: Anonymous 2007-08-13 2:55 ID:Heaven

(defun % (d)
 (
lambda (n) (= 0 (mod n d))))


(defun all (&rest predicates)
 (
lambda (x)
  (
every (function identity)
   (
mapcar (function (lambda (p) (funcall p x))) predicates))))


(all (% 3) (% 5))

Name: Anonymous 2007-08-13 3:02 ID:3J35KuIg

>>21

oh wow thanks!


(defun any (&rest predicates)
 (lambda (x)
  (some (function identity)
   (mapcar (function (lambda (p) (funcall p x))) predicates))))


did that so (any (% 3) (% 5)) makes just the function I needed,

can anyone beat that? I don't think so :p

Name: Anonymous 2007-08-13 3:12 ID:UK+mlIcu

>>15
This is what I love about you smug LISP weenies. Always making excuses for your failure of a language. Go count some parenthesis, I'm done talking with your stupid ass.

Name: Anonymous 2007-08-13 3:38 ID:Heaven

Good, now make it print "fizz" and "buzz".

Name: Anonymous 2007-08-13 3:53 ID:n8mj67Rh

yeah i dont know lisp or anything but this thread is hilarious i mean what the fuck is >>21 even supposed to mean and how does it relate to finding if numbers are evenly divisible by 3 or 5

Name: Anonymous 2007-08-13 5:45 ID:Heaven

>>25
Wow. Get the fuck out.

Name: Anonymous 2007-08-13 6:24 ID:WEd4ro21

there are some functions explained in the book On Lisp exactly like the "all" and "any" you proposed. They're called fun (function union) and fint (function intersection).

Name: Anonymous 2007-08-13 7:30 ID:3J35KuIg

>>24
lmao, that wasn't actually the program but hehe

>>25
of course you don't understand it if you don't know lisp... duh..

>>27
I, The OP, will read On Lisp then, thanks.

Name: Anonymous 2007-08-13 8:36 ID:Heaven

>>23
I can actually implement that fail syntax your language has in lisp and use it in lisp.

Name: Anonymous 2007-08-13 8:54 ID:9p+T4lNQ

Wow this shit is like perl

Many ways to do it

Name: Anonymous 2007-08-13 9:21 ID:h0iRTlfF

In Lisp how do i divide by 0 ?

Name: Anonymous 2007-08-13 9:55 ID:1e5AoIKR

>>31
(div 1 ())

Name: Anonymous 2007-08-13 11:26 ID:3J35KuIg

arithmetic error DIVISION-BY-ZERO signalled
Operation was SB-KERNEL::DIVISION, operands (1 0).
   [Condition of type DIVISION-BY-ZERO]

Restarts:
 0: [ABORT] Return to SLIME's top level.
 1: [ABORT] Exit debugger, returning to top level.

Backtrace:
  0: (SB-KERNEL::INTEGER-/-INTEGER 1 0)
  1: (/ 0)
  2: (SB-INT:SIMPLE-EVAL-IN-LEXENV (/ 0) #<NULL-LEXENV>)
  3: (SWANK::EVAL-REGION
      "(/ 0)
     "
      T)
  4: ((LAMBDA ()))
  5: ((LAMBDA (SWANK-BACKEND::FN)) #<CLOSURE (LAMBDA #) {124BC14D}>)

Name: Anonymous 2007-08-13 21:51 ID:3J35KuIg

does anyone else have the answer?

Name: Anonymous 2007-08-13 22:06 ID:Mn9xg8th

Isn't >>1's function pretty much the simplest, most clear expression possible, given the requirements? Could use a bit of indenting though, but.

Name: Anonymous 2007-08-13 22:57 ID:hsgFZZc9

(\n->any ((==0).mod n) [3,5])

(\n->n `mod` 3 == 0 || n `mod` 5 == 0)

cleverly disguised with paranthesis

Name: Anonymous 2007-08-13 23:38 ID:3J35KuIg

>>36
that reminds me, haskell is based on C

Name: Anonymous 2007-08-13 23:56 ID:DCTLBuXh

>>1

(lambda x x (lambda x x) (car (lambda x (cdr (lambda x) x ) x)))))))))))))))))))))))))))))))))))

Name: Anonymous 2007-08-14 5:39 ID:M+StMIHs

>>38
lololol

Name: Anonymous 2007-08-14 7:00 ID:iqrAANyA

divisibleBy3or5 x = divisibleBy x 3 || divisibleBy x 5
    where
    divisibleBy = (== 0) `dot` mod   
    dot = (.).(.)

Name: Anonymous 2007-08-14 8:26 ID:M+StMIHs

Name: Anonymous 2007-08-14 9:00 ID:Heaven

>>40
You forgot the paranthesis. LISP or GTFO.

divisibleBy3or5 = (\x -> (||) ((((.).(.)) (== 0) (mod)) (x) (3)) ((((.).(.)) (== 0) (mod)) (x) (5)))

Name: Anonymous 2007-08-14 10:38 ID:M+StMIHs

>>42
you forgot the CAR CDR LAMBDA and )))))))))))))))))))))))))))

Name: Anonymous 2007-08-14 13:04 ID:4If5aQmY

You people obviously have no experience writing enterprise maintainable code. See the following short snippet for an elegant yet extendible solution in Java. Note how code encapsulation lets me reuse the subtle optimizations in new code. I plan to further generalize it by wrapping it all up with the singleton and factory patterns and allowing configuration via XML.


interface NumDivisor {
    public boolean dividesNumber(int n);
}

class NumDivisor3 implements NumDivisor {
    public boolean dividesNumber(int numberToBeDivided) {
        String numberConvertedToString = String.valueOf(numberToBeDivided);
        int sumOfNumbersDigits = 0;
        if (!numberConvertedToString.startsWith("-"))   // Fix for negative numbers.  -- John.
            for (int loopIndex = 0; loopIndex < numberConvertedToString.length(); loopIndex = loopIndex + 1)
                sumOfNumbersDigits = sumOfNumbersDigits + Integer.parseInt(numberConvertedToString.substring(loopIndex, loopIndex + 1));
        else
            for (int loopIndex = 1; loopIndex < numberConvertedToString.length(); loopIndex = loopIndex + 1)
                sumOfNumbersDigits = sumOfNumbersDigits + Integer.parseInt(numberConvertedToString.substring(loopIndex, loopIndex + 1));
        if (sumOfNumbersDigits % 3 == 0)
            return true;
        else
            return false;
    }
}

class NumDivisor5 implements NumDivisor {
    public boolean dividesNumber(int numberToBeDivided) {
        String numberConvertedToString = String.valueOf(numberToBeDivided);
        if (numberConvertedToString.endsWith("5"))
            return true;
        else if (numberConvertedToString.endsWith("0"))
            return true;
        else
            return false;
    }
}

class NumDivisor3and5 implements NumDivisor {
    private NumDivisor numDivisor3 = new NumDivisor3();
    private NumDivisor numDivisor5 = new NumDivisor5();

    public boolean dividesNumber(int numberToBeDivided) {
        if (numDivisor3.dividesNumber(numberToBeDivided))
            if (numDivisor5.dividesNumber(numberToBeDivided))
                return true;
            else
                return false;
        else
            return false;
    }
}


Java Satori is within my reach.

Name: Anonymous 2007-08-14 13:37 ID:vAzUsR6T


for(i=0;i<=n;i++)
    if(i*3 == n || i*5 == n) return 1;
return 0;

Name: Anonymous 2007-08-14 13:47 ID:Heaven


<?php
$x = 1000;
echo 1.5*(int)(($x-1)/3)*(int)(($x+2)/3) + 2.5*(int)(($x-1)/5)*(int)(($x+4)/5) - 7.5*(int)(($x-1)/15)*(int)(($x+14)/15);
?>

Everyone gtfo.

Name: Anonymous 2007-08-14 17:02 ID:Heaven

>>46
winner! best algorithm in worst language

Name: Anonymous 2007-08-15 1:18 ID:418Q0+sP

>>44
um....................  O_o

Name: Anonymous 2007-08-15 2:38 ID:r5M6RbB2

>>48
nevermind him, he listens to Infected Mushroom

Name: Anonymous 2007-08-15 8:25 ID:C4krUgxi

>>44

// Fix for negative numbers.  -- John.

i fucking lold

Name: Anonymous 2007-08-15 20:43 ID:Ya7LS4Ou

>>44
Total winnage

Name: Anonymous 2007-08-15 21:33 ID:Heaven

>>47
worst?
see >>44

oh win btw

Name: Anonymous 2007-08-16 10:24 ID:GsTG7qoW

from
(lambda (n) (or (= 0 (mod n 3)) (= 0 (mod n 5))))
to
(all (% 3) (% 5))

what the fuck more do you want?

Name: Anonymous 2007-08-16 10:45 ID:Heaven

>>53
Implementation of >>46 in lisp to work with a list of numbers
eg (- (all 3 5) (all 15))

Name: Anonymous 2007-08-16 11:09 ID:GsTG7qoW

>>54
1) Why the fuck are you saging
2) What the fuck is that - for?

Name: Anonymous 2007-08-16 11:14 ID:Heaven

>>55
1) Why the fuck are you saging
makes me feel better than the rest
2) What the fuck is that - for?
read >>46 's code and you'll understand.

Actually the limit shouldn't be hardcoded
(all n &rest l)

Name: Anonymous 2007-08-16 11:17 ID:Heaven

>>55 does not understand sage.

Name: Anonymous 2007-08-16 11:52 ID:GsTG7qoW

>>56
No, you are an idiot.

Name: Anonymous 2007-08-16 13:36 ID:owrw4RNj

: divisible_by_any? ( n seq -- ? ) [ mod ] map-with product zero? ;
: divisible_by_3_or_5? ( n -- ? ) { 3 5 } divisible_by_any ;

Name: Anonymous 2007-08-16 14:10 ID:GsTG7qoW

>>59
oh shit thats impressive

Name: Anonymous 2007-08-16 14:14 ID:iyuIKdp/

Prolog faggotry

divisibleBy3or5(A):-Z is A mod 3, Z =:= 0.
divisibleBy3or5(A):-Z is A mod 5, Z =:= 0.

Name: Anonymous 2007-08-16 14:48 ID:GsTG7qoW

>>61
Nice!

also, lol @ =:=

Name: Anonymous 2007-08-16 16:30 ID:8Drr5be4

>>59

Oh god is that Factor

Name: Anonymous 2007-08-16 19:57 ID:st1bIf4i

>>59

divisiblebyany l x  = product (map (mod x) l) == 0

I still think that

divisiblebyany l x = any ((== 0).(mod x)) l

or
divisiblebyany x = any ((== 0).(mod x))

or

divisiblebyany x = any (divisibleby x)

is better though.

Name: Anonymous 2007-08-16 19:58 ID:st1bIf4i


Name: Anonymous 2007-08-16 19:59 ID:st1bIf4i

Name: Anonymous 2007-08-16 20:19 ID:owrw4RNj

>>64
: divisible_by_any? ( n seq -- ? ) [ mod zero? ] contains-with? ;

Name: Anonymous 2009-09-19 1:35

Lain.

Name: Anonymous 2009-09-19 1:35

Lain.

Name: Anonymous 2009-09-19 18:28


(define (div-by-3-or-5 n)
   (define (divisible? x y)
      (= 0 (mod x y)))
   (or (divisible? n 3) (divisible? n 5)))

Name: Anonymous 2009-09-19 18:29

>>71
too obvious

Name: Anonymous 2009-09-19 18:48

>>1
I also posted this. Nostalgia <3.

Name: Anonymous 2009-09-20 8:31

sage
your
momma's
anus

it's
oh so
sweet

like
sugar

Name: Anonymous 2009-09-20 8:33

sage
your
momma's
anus
wooo
woooer

Name: Anonymous 2009-09-20 8:34

work
you
bastard
bbcode

indentation
decreasing

Name: Anonymous 2009-09-20 8:35

how
do
i
decrease

indent
halp

Name: Anonymous 2009-09-20 8:36

my
anus
is
so^_^

elegant
hax!

Name: Anonymous 2009-09-20 8:37

my
anus
is
so^_^

elegantoh lordy[/o][o]

hax!

Name: Anonymous 2009-09-20 8:37

my
anus
is
so^_^

elegantoh lordy

hax!

Name: Anonymous 2009-09-20 8:37

my
anus
is
so^_^
ninjas
beans

Name: Anonymous 2009-09-20 8:46

y helo
thar
hax
my anus
ploxmy anus

Name: Anonymous 2009-09-20 8:47

y helo
thar
hax
my anus
ploxmy anus
is so ready
for hacking
[/o]

Name: Anonymous 2009-09-20 9:18

#include <stdio.h>

int main() {
  printf("Hello, world!\n");
  return 0;
}

Name: Anonymous 2009-09-20 9:51

Let me just EXPERT FORMAT(TM) that for you:

code]o[/code]
oo
o
o HOWo
o
o
o
o DOo
o
o
o
o
o
o YOUo
o
o
o
o
o
o
o
o FEELo
o
o
o
o
o
o
o
o ABOUTo
o
o
o
o
o
o BBCODE?o
o
o
o
o i like it!o
o
oo

Name: Anonymous 2013-08-31 20:10


 You know who wasn't stupid? The guy who invented the pet rock.

Name: Anonymous 2013-08-31 20:12

>>1
what? that is perfect, you don't need anything else
everyone else in this thread is trolling you

Name: Anonymous 2013-08-31 21:40


I made a big mistake bringing Marisa with me. There aren't any partner spellcards on the floor (only in secret rooms, but I have found only one). I prepared her to do massive damage with spellcards, so she sucks at one-on-one battle.

Don't change these.
Name: Email:
Entire Thread Thread List