上还是下Wythoff?


20

首先,让我们谈谈Beatty序列。给定一个无理数正数r,我们可以通过将正整数乘以r并乘以每个结果计算的底数来构造无限序列。例如,
Beatty sequence of r

如果r > 1,则有特殊条件。我们可以形成另一个无理数s,s = r /(r -1)。然后,这可以生成自己的Beatty序列B s。巧妙的技巧是B rB s互补的,这意味着每个正整数都恰好在两个序列之一中。

如果我们将黄金比例设置为r = then,则得到s = r + 1和两个特殊序列。该低Wythoff序列[R

1, 3, 4, 6, 8, 9, 11, 12, 14, 16, 17, 19, 21, 22, 24, 25, 27, 29, ... 

上部Wythoff序列小号

2, 5, 7, 10, 13, 15, 18, 20, 23, 26, 28, 31, 34, 36, 39, 41, 44, 47, ... 

这些分别是OEIS 上的序列A000201A001950

挑战

给定正输入整数1 <= n <= 1000,输出两个不同值之一,指示输入是处于较低的Wythoff序列还是较高的序列。输出值可以是-11truefalseupperlower,等等。

尽管理论上您提交的算法必须适用于所有输入,但实际上,它只必须使用前1000个输入数字。

I / O和规则

  • 输入和输出可以通过任何方便的方法给出。
  • 可以假定输入和输出适合您语言的本机数字类型。
  • 完整的程序或功能都是可以接受的。如果是函数,则可以返回输出而不是打印输出。
  • 禁止出现标准漏洞
  • 这是因此所有常用的高尔夫规则都适用,并且最短的代码(以字节为单位)获胜。

1
这基本上是“降低Wythoff序列的下限”,因为较高的Wythoff序列比较低的Wythoff序列需要多1个运算(平方phi)。
魔术章鱼缸

Answers:


12

JavaScript(ES6),50 35字节

f=(n,s="1",t=0)=>s[n-1]||f(n,s+t,s)
<input type=number min=1 oninput=o.textContent=this.value&amp;&amp;f(this.value)><pre id=o>

1较低和0较高的输出。说明:布尔值的部分列表可以使用来构建一个斐波那契状的身份:给定两个列表,开始110,每个后续名单是前两者的串联,导致1011011010110101等等。在这种情况下,它稍微golfier有伪造的第0个条目,0并使用该条目构造列表的第二个元素。


4
怎么样...
AdmBorkBork

4
我喜欢这种解释如何使我对+1的了解更少。布尔的部分妓女窃取了一个名叫Fibbonacci的人的身份,该人随后与他的孙子们联系在一起,假冒建筑的入口。
魔术章鱼缸

我很想知道这个33字节的版本通过近似可以工作多远。答案显然是n = 375
Arnauld

7

Haskell,26个字节

(l!!)
l=0:do x<-l;[1-x..1]

在线尝试!

无浮球,无限精度。感谢H.PWiz提供两个字节。


也将是26个字节,但我不明白为什么它不起作用
H.PWiz

@ H.PWiz我认为这是因为空列表是固定点。
xnor18年

啊,我没有考虑过,而是将其与使用的“等效”方法进行比较~(x:t)。谢谢
H.PWiz

@ H.PWiz / xnor从技术上讲,在Haskell中使用的固定点在符号上是最小的,这里是底部/ undefined。同样存在两个不同的定义事实,这只是偶然的。
与Orjan约翰森

7

Python,25个字节

lambda n:-n*2%(5**.5+1)<2

在线尝试!

使用非常简单的条件:

n恰好在时处于较低的Wythoff序列中-n%phi<1

请注意,取模结果即使-n为负也为正,与Python的取模方式相匹配。

证明:让它a = -n%phi在范围内0 <= a < phi。我们可以分割 -nphi-n = -k*phi + a一些正整数k。将其重新排列为n+a = k*phi

如果是a<1,则n = floor(n+a) = floor(k*phi),等等在较低的Wythoff序列中。

否则,我们有1 <= a < phi这样

n+1 = floor(n+a) = floor(k*phi)
n > n+a-phi = k*phi - phi = (k-1)*phi

因此n落在 和之间的间隙中,floor((k-1)*phi)floor(k*phi)较低的Wythoff序列则错过了。

这对应于以下代码:

lambda n:-n%(5**.5/2+.5)<1

在线尝试!

我们通过加倍到来节省一个字节-(n*2)%(phi*2)<2


您能解释一下公式是如何产生的吗?我试图从序列定义中派生它,但是迷路了。
sundar-恢复莫妮卡

@sundar添加了一个证明。
xnor

5

05AB1E,9个字节

L5t>;*óså

在线尝试!


0表示较高,1表示较低。尝试前100个:在线尝试!


    CODE   |      COMMAND      # Stack (Input = 4)
===========+===================#=======================
L          | [1..a]            # [1,2,3,4]
 5t>;      | (sqrt(5) + 1)/2   # [phi, [1,2,3,4]]
     *     | [1..a]*phi        # [[1.6,3.2,4.8,6.4]]
      ó    | floor([1..a]*phi) # [[1,3,4,6]]
       så  | n in list?        # [[1]]

原始命令转储:

----------------------------------
Depth: 0
Stack: []
Current command: L

----------------------------------
Depth: 0
Stack: [[1, 2, 3, 4]]
Current command: 5

----------------------------------
Depth: 0
Stack: [[1, 2, 3, 4], '5']
Current command: t

----------------------------------
Depth: 0
Stack: [[1, 2, 3, 4], 2.23606797749979]
Current command: >

----------------------------------
Depth: 0
Stack: [[1, 2, 3, 4], 3.23606797749979]
Current command: ;

----------------------------------
Depth: 0
Stack: [[1, 2, 3, 4], 1.618033988749895]
Current command: *

----------------------------------
Depth: 0
Stack: [[1.618033988749895, 3.23606797749979, 4.854101966249685, 6.47213595499958]]
Current command: ó

----------------------------------
Depth: 0
Stack: [[1, 3, 4, 6]]
Current command: s

----------------------------------
Depth: 0
Stack: [[1, 3, 4, 6], '4']
Current command: å
1
stack > [1]

我有相同的,但使用的是ï:)
Emigna

@emigna我很惊讶phi不在数学常数中。5t>;到2个字节也许不值得...
魔术

是的,我半想起那可能是(但事实并非如此)。似乎我们应该添加一些内容。
Emigna '18年

@Emigna我相当确定Jelly的答案是正确的,但是带有phi内置的哈哈。
魔术章鱼缸

哈哈,我也一样,但是使用ï¢大声笑:)我们所有的解决方案都息息相关
Xcoder先生18年

5

果冻,5个字节

N%ØpỊ

在线尝试!

多亏了xnor的Python golf,节省了1个字节。


果冻,6个字节

×€ØpḞċ

在线尝试!

对于低位返回1,对于高位返回0

×€ØpḞċ – Full Program / Monadic Link. Argument: N.
×€     – Multiply each integer in (0, N] by...
  Øp   – Phi.
    Ḟ  – Floor each of them.
     ċ – And count the occurrences of N in that list.

检查 0ñ]ž 绝对足够,因为 φ>1个ñ>0 因此 0<ñ<ñφ


I'm guessing one of those is a 1-byte constant for phi :P?
Magic Octopus Urn

2
不,一个两字节的字节:Øp
Xcoder先生,18年

呵呵,比我的05AB1E中的4字节字节更好:5t>;
魔术八爪鱼

4

Brain-Flak,78个字节

([{}]()){<>{}((([()]))){{<>({}())}{}(([({})]({}{})))}<>([{}]{}<>)}<>({}()){{}}

在线尝试!

下限和0上限不输出任何内容。更改为更合理的输出方案将花费6个字节。


4

Python 2中39个 33 32字节

得益于Xcoder先生,-6个字节得益于Zacharý得
-1个字节

lambda n,r=.5+5**.5/2:-~n//r<n/r

在线尝试!

False较低和True较高的回报


lambda n,r=(1+5**.5)/2:-~n//r<n/r saves 6 bytes.
Mr. Xcoder

1
Also, lambda n,r=.5+5**.5/2:-~n//r<n/r should work as well to shave one byte
Zacharý

3

Julia 0.6, 16 bytes

n->n÷φ<-~n÷φ

Try it online!

While playing around with the numbers, I came across this property: floor(n/φ) == floor((n+1)/φ) if n is in the upper Wythoff sequence, and floor(n/φ) < floor((n+1)/φ) if n is in the lower Wythoff sequence. I haven't figured out how this property comes about, but it gives the correct results at least upto n = 100000 (and probably beyond).


Old answer:

Julia 0.6, 31 bytes

n->n∈[floor(i*φ)for i1:n]

Try it online!

Returns true for lower and false for upper Wythoff sequence.


As n/φ of the numbers up to n are lower and the others are upper, the average difference between successive lower numbers is φ; dividing the lower numbers by φ gives you a sequence where the average difference is 1; this makes it possible for the floor of that sequence to be the integers. My maths isn't good enough to take it any further though.
Neil


1

Wolfram Language (Mathematica), 26 bytes

#~Ceiling~GoldenRatio<#+1&

Try it online!

An integer n is in the lower Wythoff Sequence iff ceil(n/phi) - 1/phi < n/phi.

Proof that ceil(n/phi) - 1/phi < n/phi is...

Sufficient:

  1. Let ceil(n/phi) - 1/phi < n/phi.

  2. Then, ceil(n/phi) * phi < n + 1.

  3. Note n == n/phi * phi <= ceil(n/phi) * phi.

  4. Hence, n <= ceil(n/phi) * phi < n + 1.

  5. Since n and ceil(n/phi) are integers, we invoke the definition of floor and state floor(ceil(n/phi) * phi) == n, and n is in the lower Wythoff sequence.

Necessary; proof by contrapositive:

  1. Let ceil(n/phi) - 1/phi >= n/phi.

  2. Then, ceil(n/phi) * phi >= n + 1.

  3. Note n + phi > (n/phi + 1) * phi > ceil(n/phi) * phi

  4. Hence n > (ceil(n/phi) - 1) * phi.

  5. Since (ceil(n/phi) - 1) * phi < n < n + 1 <= ceil(n/phi) * phi, n is not in the lower Wythoff sequence.


This also doesn't have any rounding error.
user202729

1

Japt, 10 bytes

Returns true for lower and false for upper.

õ_*MQ fÃøU

Try it online!

Explanation:

õ_*MQ fÃøU
             // Implicit U = Input
õ            // Range [1...U]
 _           // Loop through the range, at each element:
  *MQ        //   Multiply by the Golden ratio
      f      //   Floor
       Ã     // End Loop
        øU   // Return true if U is found in the collection

1
I had this for 10 bytes too.
Shaggy

1

Java 10, 77 53 52 bytes

n->{var r=Math.sqrt(5)/2+.5;return(int)(-~n/r)<n/r;}

Port of @Rod's Python 2 answer.
-1 byte thanks to @Zacharý.

Try it online.


Old 77 76 bytes answer:

n->{for(int i=0;i++<n;)if(n==(int)((Math.sqrt(5)+1)/2*i))return 1;return 0;}

-1 byte thanks to @ovs' for something I recommended myself last week.. xD

Returns 1 for lower; 0 for upper.

Try it online.

Explanation:

n->{                    // Method with integer as both parameter and return-type
  for(int i=0;++i<=n;)  //  Loop `i` in the range [1, `n`]
    if(n==(int)((Math.sqrt(5)+1)/2*i))
                        //   If `n` is equal to `floor(Phi * i)`:
      return 1;         //    Return 1
  return 0;}            //  Return 0 if we haven't returned inside the loop already

i*Phi is calculated by taking (sqrt(5)+1)/2 * i, and we then floor it by casting it to an integer to truncate the decimal.


1
++i<=n on your old answer can be i++<n.
ovs


1
I think this should work for -1 byte:n->{var r=Math.sqrt(5)/2+.5;return(int)(-~n/r)<n/r;}
Zacharý

@Zacharý It indeed does, thanks!
Kevin Cruijssen

1

Haskell, 153 139 126 79 bytes

Unlimited Precision!

l=length
f a c|n<-2*l a-c,n<0||l a<a!!n=c:a|1>0=a
g x=x==(foldl f[][1..x+1])!!0

Try it online!

Explanation

Instead of using an approximation of the golden ratio to calculate the result meaning they are prone to errors as the size of the input rises. This answer does not. Instead it uses the formula provided on the OEIS that a is the unique sequence such that

n . b(n) = a(a(n))+1

where b is the ordered compliment.


1
"All" wasn't even true before you got outgolfed...
Neil

@Neil Good point. I must have missed your answer.
Wheat Wizard

Although your answer is limited by the fact that javascript doesn't have an integral type?
Wheat Wizard

Well, it will run out of memory well before then...
Neil

1

Brachylog, 8 bytes

≥ℕ;φ×⌋₁?

Try it online!

The predicate succeeds if the input is in the lower Wythoff sequence and fails if it is in the upper Wythoff sequence.

 ℕ          There exists a whole number
≥           less than or equal to
            the input such that
  ;φ×       multiplied by phi
     ⌋₁     and rounded down
       ?    it is the input.

If failure to terminate is a valid output method, the first byte can be omitted.


This is probably the very first time φ is used in a Brachylog program. At long last!
Fatalize

0

MATL, 8 bytes

t:17L*km

Try it online!

Explanation

t      % Implicit input. Duplicate
:      % Range
17L    % Push golden ratio (as a float)
*      % Multiply, element-wise
k      % Round down, element-wise
m      % Ismember. Implicit output

0

K (oK), 20 bytes

Solution:

x in_(.5*1+%5)*1+!x:

Try it online!

Explanation:

x in_(.5*1+%5)*1+!x: / the solution
                  x: / save input as x
                 !   / generate range 0..x
               1+    / add 1
              *      / multiply by
     (       )       / do this together
           %5        / square-root of 5
         1+          / add 1
      .5*            / multiply by .5
    _                / floor
x in                 / is input in this list?

0

TI-BASIC (TI-84), 18 bytes

max(Ans=iPart((√(5)+1)/2randIntNoRep(1,Ans

Input is in Ans.
Output is in Ans and is automatically printed.
Prints 1 if input is in the lower sequence or 0 if it's in the upper sequence.

Coincidentally, this program will only run for 0<N<1000 .

Example:

27
             27
prgmCDGFA
              1
44
             44
prgmCDGFA
              0

Explanation:

max(Ans=iPart((√(5)+1)/2randIntNoRep(1,Ans    ;full program, example input: 5
                        randIntNoRep(1,Ans    ;generate a list of random integers in [1,Ans]
                                               ; {1, 3, 2, 5, 4}
              (√(5)+1)/2                      ;calculate phi and then multiply the resulting
                                              ;list by phi
                                               ; {1.618 4.8541 3.2361 8.0902 6.4721}
        iPart(                                ;truncate
                                               ; {1 4 3 8 6}
    Ans=                                      ;compare the input to each element in the list
                                              ;and generate a list based off of the results
                                               ; {0 0 0 0 0}
max(                                          ;get the maximum element in the list and
                                              ;implicitly print it

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.


0

cQuents, 5 bytes

?F$`g

Try it online!

Explanation

?         output true if in sequence, false if not in sequence
          each term in the sequence equals:

 F        floor (
  $               index * 
   `g                     golden ratio
     )                                 ) implicit
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.