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

Pages: 1-4041-

musical /prog/ challange

Name: Anonymous 2010-09-22 14:03

Create an ordered list of fractions which product of the numerator and denominator equals a specific integer n.

ex: n=60
1/60, 2/30, 3/20, 4/15, 5/12, etc....

then do it with a list of integers.

If you are fancy you can also create a scale from the fractions you've got which best matches western tempered tuning (or any you prefer).

Name: Anonymous 2010-09-22 14:07

do your own homework

Name: Anonymous 2010-09-22 14:21

that ought to be coming like any other doable challenge

Name: Anonymous 2010-09-22 14:33

This is a one-liner in Perl, JavaScript, any academic language, etc.

Name: Anonymous 2010-09-22 14:40

How is this musical??

Name: Anonymous 2010-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?

Name: Anonymous 2010-09-22 14:45

well fuck
>>4
prime factorisation plus some recursion or sorting all in one line, woot

Name: Anonymous 2010-09-22 15:00

>>7
prime factorisation plus some recursion or sorting all in one line, woot
Are you acquainted with dead dogs, faggot?

Name: Anonymous 2010-09-22 15:16

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: Anonymous 2010-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: Anonymous 2010-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.

Name: Anonymous 2010-09-22 17:14

>>11
Less ternary, more fish operator:

for (for 1..6 -> $n { $n <<=><< $(for 1..$n { $a => $n %% $^a ?? $n/$a !! 0 }) }) { .say if .value.value }

PS. http://try.rakudo.org/

Name: Anonymous 2010-09-22 18:05

>>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.

This thread is now about >>10
I guess?

Name: Anonymous 2010-09-22 19:18

>>13
Don't post while on drugs, asshole.

Name: Anonymous 2010-09-22 19:22

>>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.

>>12 is ridiculous:

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.

Name: Anonymous 2010-09-22 19:48

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 + " ");        }      }    }  }}

Name: Anonymous 2010-09-22 19:51

>>16

Beautiful.

Name: Anonymous 2010-09-22 20:42

EXPERT SLOW AS FUCK


#!/usr/bin/ruby
N=ARGV[0].to_i
Array.new(N+1){|i|[i,i>0 ?N%i:1]}.reject{|e|e[1]>0}.each{|e|puts "#{e[0]}/#{N/e[0]}"}

Name: Anonymous 2010-09-22 21:08

>>18
Ruby golf always gets me. It's got that dense code line-noise thing going on, and then Object.method() comes across as comic relief.

Name: Anonymous 2010-09-22 23:35

Java is faster than C++

derp:prog $ cat ProgChallengeFactor.java
>>16

derp:prog $ cat ProgChallengeFactor.cpp
#include <iostream>
#include <stdio.h>

using namespace std;

int main(int argc, const char *argv[]) {
  int n = atoi(argv[1]);
  for (int i=0; i <= n; i++) {
    for (int j=0; j <= n; j++) {
      if (i * j == n) {
        cout << i << "/" << j << " ";
      }
    }
  }
  return 0;
}

derp:prog $ javac ProgChallengeFactor.java; time java ProgChallengeFactor 99999
1/99999 3/33333 9/11111 41/2439 123/813 271/369 369/271 813/123 2439/41 11111/9 33333/3 99999/1
real    0m13.617s
user    0m13.588s
sys     0m0.057s

derp:prog $ g++ -fomit-frame-pointer progChallengeFactor.cpp -o prog; time ./prog 99999
1/99999 3/33333 9/11111 41/2439 123/813 271/369 369/271 813/123 2439/41 11111/9 33333/3 99999/1
real    0m31.389s
user    0m31.073s
sys     0m0.064s

Name: Anonymous 2010-09-22 23:56

>>20
Heh, nice try. The Rakudo version doesn't even take that long.

Name: Anonymous 2010-09-23 0:28

>>21
Not sure what to tell you. I ran both of these on a fairly new dual-core Apple Macbook Pro and this is what I got

Name: Anonymous 2010-09-23 1:06

Apple
I stopped reading right there.

Name: Anonymous 2010-09-23 1:44

>>23
Then the sentence you read was not grammatical

Name: Anonymous 2010-09-23 2:03

>>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.

Name: Anonymous 2010-09-23 2:04

>>22
I don't believe it.

>>23
You should stop reading /prog/ all together.

>>24
But... it would be grammatically correct.

Name: Anonymous 2010-09-23 2:16

>>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.

Name: Anonymous 2010-09-23 3:57

You may also want -momg-optimized

Name: Anonymous 2010-09-23 4:05

>>20
How about not using a moron algorithm

Name: Anonymous 2010-09-23 7:56

>>19
That's why it's the most amazing language evar

Name: Anonymous 2010-09-23 9:15

>>27
Use -O7 for maximum efficiency.

Name: Anonymous 2010-09-23 9:50

>>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.

  real    0m8.885s
  user    0m8.872s
  sys     0m0.003s


So I guess g++ is better than javac.

Name: Anonymous 2010-09-23 11:55

I stopped reading this thread at >>1.

Name: Anonymous 2010-09-23 13:34

I stopped reading this thread at >>33.

Name: Anonymous 2010-09-23 13:44

x =: 60
(,.|.)y#~0=x|~y=:>:i.x

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)

Name: Anonymous 2010-09-23 14:03

>>35
You're so full of bullshit.

Name: Anonymous 2010-09-23 14:38

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?

Name: Anonymous 2010-09-23 15:04

>>37
find the integer divisors
Redundancy detected

Name: Anonymous 2010-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.

Name: Anonymous 2010-09-23 17:11

>>39
it's simplest fraction from
Faggot.

Name: Anonymous 2010-09-23 17:49

>>40
ha, RPN gets ya'

Name: Anonymous 2010-09-23 20:23

>>39
if a fraction can be simplified it is removed

2/13500 3/9000 5/5400
wat

Name: Anonymous 2010-09-23 23:11

>>42

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]

Name: Anonymous 2010-09-23 23:24

>>43
My head is full of fuck, could probably remove the check altogether but then I'd have to figure out my own code again.

Name: Anonymous 2010-09-23 23:34

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

Name: Anonymous 2010-09-23 23:44

found this today lurking on a perl6 irc channel:

(i modified it a little, i hate one-liners)


sub fraction($n) {
  map
    { ($n/$_ ~"/$_"), ("$_/" ~ $n/$_) },
    grep { $n %% $^a },
    1..($n.sqrt.ceiling)
};

for 1..60 -> $integer {
  say "=$integer=";
  .say for @(fraction($integer).uniq)
}

Name: Anonymous 2010-09-24 0:04

>>46
Hi qw3rty.

Name: Anonymous 2010-09-24 0:14

>>47
shut up, i warn you, my mom bought me a dog.

Name: Anonymous 2010-09-24 0:16

>>48
Polecat kebabs.

Name: Anonymous 2010-09-24 0:30

$ time ./fixedProgChallenge 60896
60896 / 1 5536 / 11 1903 / 32 352 / 173 173 / 352 32 / 1903 11 / 5536 1 / 60896
real    0m0.004s
user    0m0.001s
sys     0m0.002s

Someone please explain the attractiveness of forth to me

Name: Anonymous 2010-09-24 0:46

PARI/GP
? n=input();d=divisors(n);for(i=1,#d,print1(d[i],"/",d[#d-i+1]," "))
999999
1/999999 3/333333 7/142857 9/111111 11/90909 13/76923 21/47619 27/37037 33/30303 37/27027 39/25641 63/15873 77/12987 91/10989 99/10101 111/9009 117/8547 143/6993 189/5291 231/4329 259/3861 273/3663 297/3367 333/3003 351/2849 407/2457 429/2331 481/2079 693/1443 777/1287 819/1221 999/1001 1001/999 1221/819 1287/777 1443/693 2079/481 2331/429 2457/407 2849/351 3003/333 3367/297 3663/273 3861/259 4329/231 5291/189 6993/143 8547/117 9009/111 10101/99 10989/91 12987/77 15873/63 25641/39 27027/37 30303/33 37037/27 47619/21 76923/13 90909/11 111111/9 142857/7 333333/3 999999/1
time = 0 ms.

Name: Anonymous 2010-09-24 1:08

>>48
lolol is ur dof ded?

Name: Anonymous 2010-09-24 1:15

[quote]Someone please explain the attractiveness of forth to me [/quote]
$ gforth -e " SyNtaX ( comment )"

*OS command line*:-1: Undefined word

Name: shit challenge 2010-09-24 1:54

>>39
Ok, IHBT.
if a fraction can be simplified it is removed.
How is it that 2/13500 or 3/9000 or 5/5400 can't be simplified?

And if those make the list, then why doesn't 4/6750?

Name: Anonymous 2010-09-24 2:40

>>54 please consult >>42-44

Name: Anonymous 2010-09-24 10:05

In[1]:= n = 60; d =
 Drop[Select[Divisors[n]/Reverse[Divisors[n]],
   Numerator[#]*Denominator[#] == n &], -1]

Out[1]= {1/60, 3/20, 4/15, 5/12, 12/5, 15/4, 20/3}

In[2]:= n = 60896; d =
 Drop[Select[Divisors[n]/Reverse[Divisors[n]],
   Numerator[#]*Denominator[#] == n &], -1]

Out[2]= {1/60896, 11/5536, 32/1903, 173/352, 352/173, 1903/32, 5536/11}

In[3]:= n = 99999; d =
 Drop[Select[Divisors[n]/Reverse[Divisors[n]],
   Numerator[#]*Denominator[#] == n &], -1]

Out[3]= {1/99999, 9/11111, 41/2439, 271/369, 369/271, 2439/41, 11111/9}

In[4]:= n = 1285178630; d =
 Drop[Select[Divisors[n]/Reverse[Divisors[n]],
   Numerator[#]*Denominator[#] == n &], -1]

Out[4]= {1/1285178630, 2/642589315, 5/257035726, 10/128517863, \
7547/170290, 15094/85145, 17029/75470, 34058/37735, 37735/34058, \
75470/17029, 85145/15094, 170290/7547, 128517863/10, 257035726/5, \
642589315/2}


- sent from my mathematica

Name: Anonymous 2010-09-24 15:00

So they're still messing with this even without qw3rty around:

sub fn($n) {
    map
      { (($n/$_)/$_),  ($_/($n/$_)) },
      grep { $n %% $^a },
      ^($n.sqrt.ceiling)
};

.perl.say for @(fn(60).flat.sort);


Introspection on the Rational type is being abused to print fractions in reduced form.

Name: Anonymous 2010-09-24 15:51

>>57

We should throw them a difficult problem then :)

I would like to see some junctions/laziness/metaoperators in action....

Name: Anonymous 2010-09-24 17:25

>>58
There's a metaop in >>12. According to the periodic table of ops, '=>' is "fiddly", and so meta '=>' is illegal:

http://glyphic.s3.amazonaws.com/ozone/mark/periodic/Periodic%20Table%20of%20the%20Operators%20A4%20300dpi.jpg

The table must be outdated, since '%' is listed as "iffy" (i.e. you can !%), but if you try that, Rakudo rejects it and prompts you to use %% instead.

You want junctions? Here's a variation from perl6-examples:

sub blackjack (@cards) {
  given [+] map { $^a < 11 ?? $a !! $a|1 }, @cards {
    when 21 { return 'blackjack!' };
    when * < 21 { return 'ok.'  };
    when True { return 'bust!' };
  }
}

say blackjack(<1 2 3>);
say blackjack(<11 10 10>);
say blackjack(<11 11 10 10>);


There's more interesting stuff happening in the original: http://github.com/perl6/perl6-examples/blob/master/games/blackjack.p6 notably:

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:

sub pascal { [1], -> @p { [0, @p Z+ @p, 0] } ... * }
 
.say for pascal[^10];

Name: Anonymous 2010-09-24 21:01

>>59
Correction. On taking a closer look, the pair constructor ('=>') is expressly "!fiddly", which makes more sense.

Name: Anonymous 2010-12-23 7:56


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