This is a one-liner in Perl, JavaScript, any academic language, etc.
Name:
Anonymous2010-09-22 14:40
How is this musical??
Name:
Anonymous2010-09-22 14:41
>>1 If you are fancy you can also create a scale from the fractions you've got which best matches western tempered tuning
Who would want to match that?
I wouldn't say it was musical, if a scale can be created in the case that ``you are fancy''. Also this challenge is... not so challenging.
Name:
Anonymous2010-09-22 16:02
I could have written: Implement eulers "gradus suavitatis" in reverse. Find every interval which corresponds to every gradus value and create a scale from it which has x tones per octave and an overall gradus of y for z octaves.
But I anticipated ``wut?'' so I thought we might start out easy.
So do this then ``faggots''
Name:
Anonymous2010-09-22 16:10
>>7 prime factorisation
recursion
sorting
Not necessary: for 1..60 -> $n { say "n=$n"; for 1..$n { $n %% $^a ?? say "\t$a/" ~ $n/$a !! 0 } }
There's a much cleaner way... but Rakudo doesn't always accept valid Perl code and I'm sick of prodding it into submission. I can't wait for Christmas. Note my heinous use of ternary in void context. Don't do it.
>>11-12
I am willing to try this out, the try. url doesn't load for me so I wonder if what is finished first me trying to understand this syntax or portage emerging 20MB of sources.
If it does what I think it does you'll have a lot of unnecessary divisions by trying to short out every fraction and if it does skipping it. But I guess this is alrite, haven't thought of that.
>>13
try.rakudo takes a while to load sometimes, but give it a minute and you can usually get it to load.
>>11 is simple enough: for 1..60 -> $n { # $n, the "list" from >>1 is 1..60
say "n=$n";
for 1..$n { # $^a introduces $a as placeholder for $_ (1..$n)
$n %% $^a ?? say "\t$a/" ~ $n/$a !! 0
}
}
It is sub-optimal, but Rakudo probably wouldn't run it any faster using a "better" algorithm. It seems that doing simple things in place is not such a big deal vs. collecting a data set and processing it separately, so the redundancy is a non-issue for small n in practice.
for ( # list from closure:
for 1..6 -> $n { # which is another for
# «=>«: create pair $n => element for each element
$n <<=><< $( # again with the closures, $ is scalar context
for 1..$n { # form another pair, return is $a => $n/$a, this is paired with $n above
$a => $n %% $^a ?? $n/$a !! 0
})
}) { .say if .value.value } # $a => ($n => $n/$a), unless test assigned 0 instead
This one runs slow indeed, and the output is just tab delimited. Chaining pairs is almost always pointless, and probably a sign of "doing it very wrong". Maybe some ==> nonsense could be used to make either of these better, perhaps even faster. I don't really know much about the channeling stuff.
public class ProgChallenge { public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int i=0; i <= n; i++) { for (int j=0; j <= n; j++) { if (i * j == n) { System.out.print(i + "/" + j + " "); } } } }}
>>20
First off, all you did was omit frame pointers. You specified no optimizations. Secondly, Java performs a lot of those optimizations without any specifications.
Thirdly, it's possible that Java optimizes the int j initialization to outside of the loop, whereas C/C++ will not do this for you. There's some time wasted in allocating/destroying j for every iteration of i. The other two are far more pressing issues for comparison, though.
You're basically testing unoptimized compilation versus unoptimized execution, it's obvious which should be the victor.
>>25
Whoops, -O0 is no optimizations. -o is still a very low level of optimization. You want at least -O2, which does not do any optimizations that have a space/performance tradeoff, and more likely -O3, which is an aggressive optimization for maximum performance.
>>25 >>20 here, I implemented your suggestions. Moving the declaration of i and j outside the loop didn't make any difference, but -O1 caused the C++ version to be about 1.5x faster than Java.
From right to left:
i.x range 0 to x-1
>: increment all -> (1 to x)
y=: assign to y
x|~ all mod x
0= map: equals zero?
y#~ select these from y
|. reverse
,. zip
(...) (f g) x <=> x f (g x)
Isn't this challenge equivalent to saying "find the integer divisors of an integer?" The fuck does it have to do with music, sorting, recursion, or anything else?
>>37 find the integer divisors
Redundancy detected
Name:
Anonymous2010-09-23 17:09
OP here, I intended to say "Find every rational number written in it's simplest fraction form which product of the numerator and denominator equals a specific integer n."
Since everybody likes cryptic code this is the slow algorithm everybody uses.
In [spolier]FORTH[/spoiler]
: ?SIMPLYFY 2DUP MAX >R MIN DUP 2/ 1+ 2 DO DUP I MOD J I MOD OR IF ELSE DROP I UNLOOP RDROP EXIT THEN LOOP DROP RDROP FALSE ;
: PRINTFRACTION s>d 0 d.r ." /" . ;
: FINDFRACTIONS DUP 2 DO DUP 2 DO DUP I J * = IF I J MIN 3 > IF I J ?SIMPLYFY ELSE FALSE THEN IF ELSE I J PRINTFRACTION THEN THEN LOOP LOOP DROP ;
: FRACTIONS DUP DUP 1 PRINTFRACTION FINDFRACTIONS 1 SWAP PRINTFRACTION ;
Slow as fuck, but its works as intended if a fraction can be simplified it is removed.
time gforth-fast musical.fs -e " 27000 fractions bye"
1/27000 2/13500 3/9000 5/5400 8/3375 27/1000 125/216 216/125 1000/27 3375/8 5400/5 9000/3 13500/2 27000/1
real 0m27.278s
user 0m17.737s
sys 0m0.049s
And nobody done a version which merges several fraction sequences or eulers thing either (on which this idea is based on btw)
I might do a full implementation in C since nobody else seems willing to do it.
fuck, removed the 2/ in ?SIMPLYFY and replaced the 3 in FINDFRACTIONS with 1
nfi why I did that. [code]
time gforth-fast musical.fs -e " 27001 fractions bye"
1/27001 13/2077 31/871 67/403 403/67 871/31 2077/13 27001/1
real 0m20.530s
user 0m17.663s
sys 0m0.046s
[code]
well here it is if anybody still cares
: ?SIMPLYFY 2DUP MAX >R MIN DUP 1+ 2 DO DUP I MOD J I MOD OR IF ELSE DROP I UNLOOP RDROP EXIT THEN LOOP DROP RDROP FALSE ;
: PRINTFRACTION s>d 0 d.r ." /" . ;
: FINDFRACTIONS DUP 2 DO DUP 2 DO DUP I J * = IF I J ?SIMPLYFY IF ELSE I J PRINTFRACTION THEN THEN LOOP LOOP DROP ;
: FRACTIONS DUP DUP 1 PRINTFRACTION FINDFRACTIONS 1 SWAP PRINTFRACTION ;
time gforth-fast musical.fs -e " 60896 fractions bye"
1/60896 11/5536 32/1903 173/352 352/173 1903/32 5536/11 60896/1
real 1m59.988s
user 0m45.905s
sys 0m46.778s
my @values = (ace => 1|11, two => 2, three => 3, four => 4, five => 5, six => 6, seven => 7, eight => 8, nine => 9, ten => 10, jack => 10, queen => 10, king => 10, );
my @suites = < spades clubs diamonds hearts >;
my @deck = ( @values X @suites ).map: { my ($name, $value) = $^a.kv; $name ~= " of $^b"; $name => $value };
Here the deck is the Cartesian product of Ace .. King with the various suits. In other versions I've seen the deck sorted in place with: @deck.=pick(*) (i.e. @deck = @deck.pick(@deck.elems))
There was a big fuss about this, which lazily evaluates Pascal's triangle: