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

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


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