| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Vyxal 3 | 250919T135509Z | Themooni |
| 034 | jq | 250914T183410Z | pmf |
| 024 | APLNARS | 250914T091136Z | Rosario |
| 029 | TIBASIC TI84 Plus CE Python | 250912T171555Z | madeforl |
| 077 | Tcl | 180428T221306Z | sergiol |
| 052 | Scala 3 | 231118T042132Z | 138 Aspe |
| 008 | Japt | 180428T210224Z | Shaggy |
| 4875 | Vyxal G | 231114T142013Z | pacman25 |
| 031 | dc | 180621T195644Z | brhfl |
| nan | These answers are not the most efficient ways to solve the problem | 180621T160531Z | Wheat Wi |
| 056 | C gcc | 180428T202455Z | Jonathan |
| 034 | JavaScript | 180428T211838Z | Shaggy |
| 038 | Ruby | 180501T094329Z | Kirill L |
| 049 | Python 3 | 180501T154158Z | Reesi82 |
| 081 | Forth gforth | 180430T163252Z | reffu |
| 027 | Perl 6 | 180429T182907Z | nwellnho |
| 009 | Jelly | 180429T120039Z | Martin E |
| 026 | APL+WIN | 180429T094427Z | Graham |
| 171 | mIRC Scripting | 180429T074806Z | jaytea |
| 040 | JavaScript ES6 | 180428T194353Z | Neil |
| 032 | Haskell | 180428T190907Z | Angs |
| 005 | 05AB1E | 180428T195742Z | Mr. Xcod |
| 037 | R | 180428T195035Z | Giuseppe |
| 010 | Pyth | 180428T194342Z | Mr. Xcod |
| 042 | Python 3 | 180428T194113Z | Dennis |
| 042 | Ruby | 180428T184950Z | Asone Tu |
| 022 | J | 180428T184604Z | Galen Iv |
| 007 | Jelly | 180428T190017Z | Jonathan |
| 009 | Husk | 180428T190041Z | ბიმო |
Vyxal 3, 7 bytes
kNJʎ√⌊e
kNJʎ√⌊e
kN # natural numbers
J # and 0
ʎ # filtered by
√⌊e # floor(sqrt(n))%2 == 0
💎
Created with the help of Luminespire.
jq, v1.4+, 34 bytes
Using the formula from @Reesi82's approach:
(.*8+1|sqrt+1|./4|floor|2*.*.+.)+.
jq, v1.5+, 39 bytes
Using straightforward sequence filtering:
nth(.;0|recurse(.+1)|select(sqrt%2==0))
APL(NARS), 24 chars
{⍵+k+2×k×k←⌊4÷⍨1+√1+8×⍵}
It seems the formula of post https://codegolf.stackexchange.com/a/163918/120560 is right. test:
{⍵+k+2×k×k←⌊4÷⍨1+√1+8×⍵}0,⍳50
0 4 5 6 7 8 16 17 18 19 20 21 22 23 24 36 37 38 39 40 41
42 43 44 45 46 47 48 64 65 66 67 68 69 70 71 72 73
74 75 76 77 78 79 80 100 101 102 103 104 105
TI-BASIC (TI-84 Plus CE Python), 29 bytes
Input N
While J<N
G+1→G
J+not(fPart(.5int(√(G→J
End
G
Tcl, 77 bytes
proc S {x n\ 0 i\ 1} {while \$i<$x {if 1>int([incr n]**.5)%2 {incr i}}
set n}
Vyxal G, 39 bitsv2, 4.875 bytes
≬√⌊₂l
Bitstring:
111111000101100000001111100000100100000
lol vyncode
dc, 31 bytes
sn_1[d]sD[1+dv2%0=Dzln!<M]dsMxp
Try it online! (1-indexed)
Rather simple. D is a macro that just duplicates the top-of-stack. We begin by storing the user's input in n, and placing negative 1 (_1) on the stack. Macro M increments the top-of-stack by one, duplicates it, and then does the necessary sqrt mod 2 (v2%). dc's precision is zero by default, so doing a square root is, by default, actually the floor of the square root. 0=D compares our v2% to zero, and runs our duplication macro (D) if true. z fetches the stack depth, and then ln!<M compares this to n (our target), continuing to run M until we get to the value we were looking for. Finally, print it.
These answers are not the most efficient ways to solve the problem, however I do think they are interesting.
Haskell, 37 33 bytes
(!!)$do x<-[0,2..];[x^2..x^2+2*x]
Haskell, 45 42 bytes
y#x=not$y^2>x||(y+1)#x
(filter(0#)[0..]!!)
C (gcc), 57 56 bytes
- Saved a byte thanks to ceilingcat; golfed
e>=++b*btoe/++b/b.
b,e;r(g){for(e=0;g&&++e;b&1&&--g)for(b=0;e/++b/b;);g=e;}
JavaScript, 36 34 bytes
0-indexed
n=>(g=i=>i**.5&1||n--?g(++i):i)(0)
2 bytes saved thanks to l4m2
Test it
o.innerText=[...Array(50).keys()].map(
n=>(g=i=>i**.5&1||n--?g(++i):i)(0)
).join`, `
<pre id=o></pre>
Ruby, 39 38 bytes
->n{(0..n*4).select{|x|x**0.5%2<1}[n]}
0-indexed. Instead of simple looping, this approach takes a large enough sequence of numbers and selects only those that fulfill the condition. The particular multiplier n*4 is necessary to cover the case n=1 => 4 (n*n doesn't fit here), and due to the pattern in the sequence, the value of the nth number fluctuates around n*2, so taking n*4 should always be large enough.
Python 3, 49 bytes
def f(n):
k=(1+(8*n+1)**.5)//4
return n+2*k*k+k
A slightly different approach. First, if we subtract the sequence 0,1,2,... from the sequence of Schlosberg numbers, we get the sequence 0,3,3,3,3,3,10,10,10,10,10,10,10,10,10,21. Ignoring repetitions, the sequence 0,3,10,21,... is the sequence a(k) = 2k^2+k. If we can find k, the nth Schlosberg number is then n+a(k).
Given k, each a(k) is repeated 4k+1 times. Summing the length of the first k blocks, we get that the kth block of repetition ends at entry n=2k^2-k. Inverting this, we get k=(1+sqrt(8n+1))/4, which gives an explicit formula for k.
Forth (gforth), 81 bytes
Sequence is 1-indexed [f(1) is 0]
: f 0 0 begin over s>f fsqrt f>s 1 and 0= - 1 under+ dup 3 pick = until drop 1- ;
Explanation
0 0 \ place the loop index and sequence index on the stack
begin \ start an indefinite loop
over \ grab a copy of the loop index
s>f sqrt f>s \ get the square root of the index and truncate
1 and 0= \ determine if result is even
- \ if even, add 1 to sequence tracker (-1 is default for "true" in forth)
1 under+ \ add 1 to the loop index
dup 3 pick = \ check if the current sequence position is the one that was requested
until \ end the loop if condition above is true
drop 1- \ drop the loop index and subtract 1 from the loop index to get the result
Jelly, 9 bytes
ḤḶ²Ḷœ^/ị@
Unfortunately it's longer than the other answer, but I do like this approach.
Explanation
The goal is to get the ranges from each even square to each subsequent odd square, excluding the odd square. We can do this by taking the symmetric set difference of some even number of ranges from zero to just below each square.
Ḥ Double the input N to ensure it's even.
Ḷ Lowered range, from 0 to 2N-1.
² Square to get the first 2N squares.
Ḷ Get the lowered ranges up to each square.
œ^/ Fold symmetric set difference over the ranges.
ị@ Select the Nth value.
APL+WIN, 29 26 bytes
3 bytes saved thanks to Cows quack
Prompts for integer input:
(0,(~2|⌊r*.5)/r←⍳n×3)[n←⎕]
Explanation:
[n←⎕] prompts for input and selects the nth value of the result
(0,(~2|⌊r*.5)/r←⍳n×3)[n←⎕] create a vector of all results up to n×3
which works up to limit of machine. ×2 works up to n~10E7
mIRC Scripting, 171 bytes
a {
%s = 0
%x = -1
while 1 {
inc %x 2
%i = %x
while %i {
echo - %s
inc %s
dec %i
}
inc %x 2
inc %s %x
}
}
I didn't yet see this method for generating new Schlosberg numbers, so above is just a simple one that reads like pseudocode. We start at 0, display 1 number, increase 3, display 5, increase 7, display 9, etc. A simple pattern emerges.
JavaScript (ES6), 41 40 bytes
f=(n,i=0,j=1)=>n<j?n+i:f(n-j,i-2*~j,j+4)
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0
Edit: Saved 1 byte thanks to @JonathanFrech.
Pyth, 10 bytes
e.fiI2s@Z2
Explanation
e.fiI2s@Z2 – Full program.
.f – Find the first N positive integers satisfying the requirements (var: Z).
e – And take the last one (i.e. the Nth).
@Z2 – Take the square root of Z.
s – Convert to an integer.
iI2 – Check if 2 is invariant over applying GCD with the above (i.e. is it even?)
Jelly, 7 bytes
½ḞḂ¬Ʋ#Ṫ
A full program accepting n from STDIN
How?
½ḞḂ¬Ʋ#Ṫ - Main link: no arguments
# - take input (n) from STDIN & count up (i=0,1,...) collecting truthy results of:
Ʋ - last four links as a monad:
½ - square root
Ḟ - floor (½Ḟ could also be ƽ - integer square root)
Ḃ - bit (modulo by 2 - i.e. isOdd?)
¬ - logical NOT
Ṫ - tail (get the nth rather than the first n)
- implicit print to STDOUT
Husk, 9 bytes
!fȯ¦2⌊√ΘN
Explanation
!f(¦2⌊√)ΘN -- 1-indexed number, eg: 4
ΘN -- natural numbers with 0: [0,1,2,3,4,5,6,7,8,9..
f( ) -- filter by
( √) -- | square root: [0,1,1.414213562373095,1.7320508075688774,2,2.23606797749979,2.449489742783178,2.6457513110645903,2.82842712474619,3,..
( ⌊ ) -- | floor: [0,1,1,1,2,2,2,2,2,3,..
(¦2 ) -- | divisible by 2: [1,0,0,0,1,1,1,1,1,0,..
-- : [0,4,5,6,7,8,..
! -- get element: 6