Divisible by 3 or 5, in lisp
1
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/
41
Name:
Anonymous
2007-08-14 8:26
ID:M+StMIHs
>>40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40
WHAT THE FUCK IS THIS GOBSHITE
42
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)))
43
Name:
Anonymous
2007-08-14 10:38
ID:M+StMIHs
>>42
you forgot the CAR CDR LAMBDA and )))))))))))))))))))))))))))
44
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.
45
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;
46
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.
47
Name:
Anonymous
2007-08-14 17:02
ID:Heaven
>>46
winner! best algorithm in worst language
48
Name:
Anonymous
2007-08-15 1:18
ID:418Q0+sP
>>44
um.................... O_o
49
Name:
Anonymous
2007-08-15 2:38
ID:r5M6RbB2
>>48
nevermind him, he listens to Infected Mushroom
50
Name:
Anonymous
2007-08-15 8:25
ID:C4krUgxi
>>44
// Fix for negative numbers. -- John.
i fucking lold
51
Name:
Anonymous
2007-08-15 20:43
ID:Ya7LS4Ou
52
Name:
Anonymous
2007-08-15 21:33
ID:Heaven
>>47
worst?
see
>>44
oh win btw
53
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?
54
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))
55
Name:
Anonymous
2007-08-16 11:09
ID:GsTG7qoW
>>54
1) Why the fuck are you saging
2) What the fuck is that - for?
56
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)
57
Name:
Anonymous
2007-08-16 11:17
ID:Heaven
>>55 does not understand sage.
58
Name:
Anonymous
2007-08-16 11:52
ID:GsTG7qoW
>>56
No, you are an idiot.
59
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 ;
60
Name:
Anonymous
2007-08-16 14:10
ID:GsTG7qoW
>>59
oh shit thats impressive
61
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.
62
Name:
Anonymous
2007-08-16 14:48
ID:GsTG7qoW
>>61
Nice!
also, lol @ =:=
63
Name:
Anonymous
2007-08-16 16:30
ID:8Drr5be4
>>59
Oh god is that Factor
64
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.
65
Name:
Anonymous
2007-08-16 19:58
ID:st1bIf4i
66
Name:
Anonymous
2007-08-16 19:59
ID:st1bIf4i
67
Name:
Anonymous
2007-08-16 20:19
ID:owrw4RNj
>>64
: divisible_by_any? ( n seq -- ? ) [ mod zero? ] contains-with? ;
68
Name:
Anonymous
2009-09-19 1:35
Lain.
69
Name:
Anonymous
2009-09-19 1:35
Lain.
71
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)))
72
Name:
Anonymous
2009-09-19 18:29
73
Name:
Anonymous
2009-09-19 18:48
>>1
I also posted this. Nostalgia <3.
74
Name:
Anonymous
2009-09-20 8:31
sage
your
momma's
anus
it's
oh so
sweet
like
sugar
75
Name:
Anonymous
2009-09-20 8:33
sage
your
momma's
anus
wooo
woooer
76
Name:
Anonymous
2009-09-20 8:34
work
you
bastard
bbcode
indentation
decreasing
77
Name:
Anonymous
2009-09-20 8:35
how
do
i
decrease
indent
halp
78
Name:
Anonymous
2009-09-20 8:36
my
anus
is
so ^_^
elegant
hax!
79
Name:
Anonymous
2009-09-20 8:37
my
anus
is
so ^_^
elegant oh lordy[/o] [o]
hax!
80
Name:
Anonymous
2009-09-20 8:37
my
anus
is
so ^_^
elegant oh lordy
hax!
Newer Posts