Pertimbangkan urutan nomor berikut:
Ini menghitung semua fraksi biner dalam interval satuan .
(Untuk membuat tantangan ini lebih mudah, elemen pertama adalah opsional: Anda dapat melewati dan mempertimbangkan urutan dimulai dengan 1/2.)
Tugas
Tulis program (selesaikan program atau fungsi) yang ...
Pilih salah satu dari perilaku ini:
- Input n, output elemen n dari urutan (0-diindeks atau 1-diindeks);
- Input n, output pertama n elemen urutan;
- Masukkan apa-apa, output urutan nomor tak terbatas yang dapat Anda ambil dari satu per satu;
Aturan
- Program Anda setidaknya harus mendukung 1000 item pertama;
- Anda dapat memilih untuk menghasilkan desimal, atau fraksi (bawaan, bilangan bulat integer, string) sesuka Anda;
- Input / Output sebagai digit biner tidak diperbolehkan dalam pertanyaan ini;
- Ini adalah kode-golf , kode terpendek menang;
- Celah standar tidak diijinkan.
Testcases
input output
1 1/2 0.5
2 1/4 0.25
3 3/4 0.75
4 1/8 0.125
10 5/16 0.3125
100 73/128 0.5703125
511 511/512 0.998046875
512 1/1024 0.0009765625
Contoh-contoh ini didasarkan pada urutan 0-diindeks dengan 0 terkemuka disertakan. Anda perlu menyesuaikan input untuk menyesuaikan solusi Anda.
Baca lebih lajut
- OEIS A006257
- Masalah Josephus: . (Sebelumnya M2216)
- 0, 1, 1, 3, 1, 3, 5, 7, 1, 3, 5, 7, 9, 11, 13, 15, 1, 3, 5, ...
- OEIS A062383
- : untuk n > 0 , a n = 2 ⌊ l o g 2 n + 1 ⌋ atau a n = 2 a ⌊ n.
- 1, 2, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 32, 32, 32, ...
A006257 (n) / A062383 (n) = (0, 0,1, 0,01, 0,11, 0,001, ...) menghitung semua fraksi biner dalam interval satuan [0, 1). - Fredrik Johansson, 14 Agustus 2006
"1/2" "1/4" "1/8"...
take
n elemen dari itu nanti.int
s, ataudouble
dalam bahasa / implementasi di manadouble
menggunakan format IEEE binary64 ? Saya harap Anda tidak bermaksud harus mengurai string ASCII jika kami ingin mengambil input integer? Tipe integer normal adalah biner dalam bahasa seperti C. Atau apakah maksud Anda input / output tidak boleh berupa array atau string integer atau nol ASCII?Jawaban:
Haskell , 25 byte
Cobalah online!
Output desimal, satu-diindeks tanpa istilah nol awal.
Menambahkan 0,5 ke input, lalu membagi dua hingga hasilnya di bawah 2, lalu kurangi 1. Menggunakan ekspresi pointfree menghemat 1 byte lebih dari
sumber
Java 10,
6864 byteCoba pertama kali di golf kode!
Opsi 1: temukan n elemen ke- (1-diindeks)
-4 byte terima kasih kepada @Kevin Cruijssen
Ini adalah metode anonim yang menemukan istilah ke- n dengan menghapus bit paling signifikan dari n , menggandakannya dan menambahkannya, kemudian membaginya dengan kekuatan tertinggi berikutnya 2.
Cobalah online!
Panduan kode:
Akan mengedit jika perlu untuk mencetak nilai akhir alih-alih mengembalikannya.
sumber
{}
setelah loop dapat menjadi sebagai;
gantinya; Anda dapat menghapus ruang setelahreturn
;2.0
bisa2.
; Dan mengubahn>>x!=1;x++
,1<<x
dan1<<x+1
untukn>>x++!=1;
,1<<x-1
,1<<x
masing-masing juga menghemat byte. Cobalah online: 64 byte . Selamat menikmati!MathGolf ,
54 byteCobalah online!
Bagaimana kelihatannya dengan operator yang bekerja dengan benar
Cobalah online!
Penjelasan
Saya mengambil inspirasi dari pertanyaan ini untuk menyelesaikan masalah, saya kira solusi "sendiri" sekitar 10-12 byte.
Saya bermaksud untuk putaran ke kekuatan terdekat 2 untuk mengembalikan nomor itu sendiri jika nomor dua, tetapi karena kesalahan itu putaran ke kekuatan dua berikutnya (misalnya 4 -> 8 bukannya 4 -> 4 ). Ini harus diperbaiki nanti, tetapi sekarang menghemat satu byte.
sumber
]
melayani tidak ada tujuan lain selain memformat output, saya akan mengatakan Anda tidak perlu memasukkannya dalam jumlah byte Anda.Java 10,
8985706968 bytesPort of @Emigma's 05AB1E answer, so outputs decimals indefinitely as well.
-15 bytes thanks to @Arnauld.
Try it online.
Explanation:
sumber
Perl 6, 19 bytes
Try it online!
sumber
Python 3, 33 bytes
Try it online!
Outputs decimals, one-indexed without the initial zero term.
sumber
Java (JDK 10), 30 bytes
Try it online!
Returns the nth item in the sequence.
This answer is originally a succession of golfs of TCFP's Java answer. In the end, the golfs didn't look like the original answer anymore (though the math used is the same) so I decided to post the golfs as a separate answer instead of simply commenting on the TCFP's answer. So if you like this answer, go upvote TCFP's answer as well! ;-)
Intermediate golfs were:
sumber
05AB1E,
118 bytesSaved 3 bytes thanks to Kevin Cruijssen.
Try it online!
Explanation
sumber
∞
(infinite list starting at 1):∞oεDÅÉs/}˜
[1,2,4,4,8,8,8,8,16,16,...,2**n]
and prepending the correct indexed prime followed by a/
... But that wasn't working so well. Well, but not8-bytes
well. Something like9LoDÅP)ζ
.Jelly, 9 bytes
Try it online!
sumber
PowerShell, 40 bytes
Try it online!
Outputs the infinite sequence as decimal values. Given language limitations, will eventually run into precision problems, but easily handles the first 1000 entries.
Starts by setting
$i=2
, then enters afor
loop. Each iteration, we construct a range from1..$i
and pull out the odd values with|?{$_%2}
. Those are fed into their own inner loop, where we divide each to get the decimal|%{$_/$i}
. Those are left on the pipeline and output when the pipeline is flushed after everyfor
iteration. Each iteration we're simply incrementing$i
by$i*=2
to get the next go-round.sumber
Haskell,
3532 bytesEdit: -3 bytes thanks to @Delfad0r.
This is an infinite list of integer pairs.
Try it online!
sumber
Haskell, 40 bytes
Try it online!
Infinite sequence as pairs of integers (starting from
(1,2)
).Quite a bit longer than @nimi's answer, but the approach is completely different, so I decided to post it anyway.
This solution is based on the following observation.
Consider the infinite sequence{12,14,34,18,38,58,78,116,316,…}
and apply the following steps.
Notice how you get back to the sequence you started with!
The solution exploits this fact (together with Haskell's laziness) to compute the sequence
s
.sumber
Python 2 -
6866 bytes-2 bytes thanks to Kevin
Try it Online!
sumber
return 2*(n-a)
toreturn(n-a)*2
. And you could save an additional byte by using Python 2 instead of 3, soreturn
can beprint
(with parenthesis).len
andbin
instead oflog
.Python 3,
5351 bytesn
.Try it online!
sumber
def f(m=2,n=1):n<m and print(n/m)&f(m,n+2)or f(m+m)
R, 42 bytes
Try it online!
Returns a pairN=2∗(n−2⌊log2(n)⌋)+1 from the Josephus sequence and D=2⌊log2(n)⌋+1 from the other sequence. Happily we are able to re-use the denominator as the two formulas have quite a lot in common!
Denominator,Numerator
. Uses the formulasumber
Racket,
9291 bytesTry it online!
sumber
MATL, 8 bytes
Try it online!
Returns Numerator, then Denominator. Uses the same method as my R answer, although it's a bit more efficient.
Explanation, with input
5
:sumber
Shakespeare Programming Language, 426 bytes
Try it online!
Outputs the sequence infinitely as both numbers separated by a space, with each item being separated by a newline.
sumber
You be twice the sum of a cat
Python 2, 44 bytes
Try it online!
Function returns a tuple of (numerator, denominator). An input of 0 is not handled (it was optional).
sumber
return 2*n-m+1,m
can beprint-~n+n-m,m
to save 2 bytes.Excel
4828 BytesSaved 20 bytes (!) thanks to tsh
=MOD(A1+0.5,2^(INT(LOG(A1,2))))/2^INT(LOG(A1,2))Assumes value in A1, output is in decimal. If you want the output to be in fraction, you can create a custom format for the output cell as "0/###0" and it will show it as fraction.
Explanation: Difficult to explain, since there is a shortcut taken to get to this formula. Basically the numerator is a bit shift left of the input, and the denominator is the next power of 2 higher than the number input.
I originally started with Excel built in functions for BITLSHIFT and BITRSHIFT, but they will shift the entire 48 bits which is not what you want. The functions DEC2BIN (and BIN2DEC) have a limit of -512 to 511 (10 bits) so this wouldn't work. Instead I had to rebuild the number with a modulus of the original number, then times two, then add 1 (since the left digit would always be 1 before a shift).
Examples:
sumber
=(A1+0.5)/2^INT(LOG(A1,2))-1
?C++,
977571 bytes-26 bytes thanks to tsh, ceilingcat, Zacharý
Testing code :
sumber
if(!i)return 0;
since 0 is not required in the challenge.while
but tryfor
.for(;exp;)
is as same aswhile(exp)
but you can write two more other statement into it. Prefer?:
instead ofif else
, which would be shorter in most cases.(...)
aroundd-n-1
.C (gcc), 63 bytes
No input, prints infinite sequence:
Try it online!
sumber
JavaScript (ES6), 44 bytes
Returns then -th term, 1-indexed.
Try it online!
sumber
Ruby, 42 bytes
Try it online!
Prints integer pairs infinitely, starting from 1/2.
sumber
JavaScript (Node.js), 30 bytes
Try it online! 0-indexed. Started out as a port of my Batch answer but I was able to calculate in multiples of12 which saved several bytes.
sumber
Ruby, 31 bytes
Try it online!
sumber
><>,
1918 bytesUsing xnor's idea, fixed by Jo King, -1 byte by making better use of the mirrors and another -2 bytes by Jo King because the
!
was superfluous and;
is not required.Try it online!
sumber
-0.25
. Fix for the same amount of bytesWolfram Language (Mathematica), 22 bytes
Try it online!
sumber
APL (Dyalog Unicode), 15 bytes
Try it online!
Anonymous prefix lambda.
Thanks to Adám for 4 bytes and to Cows quack for 2 bytes.
How:
sumber
C# (.NET Core), 69 bytes
Try it online!
Ungolfed:
sumber