Questo numero è loeschiano?


33

Un numero intero positivo kè un numero loeschiano se

  • kpuò essere espressa come i*i + j*j + i*jper i, jnumeri interi.

Ad esempio, i primi numeri loeschiani positivi sono: 1( i=1, j=0); 3( i=j=1); 4( i=2, j=0); 7( i=2, j=1); 9( i=-3, j=3); ... Nota che i, jper un dato dato, knon sono univoci. Ad esempio, 9possono anche essere generati con i=3, j=0.

Altre caratterizzazioni equivalenti di questi numeri sono:

  • kpuò essere espressa come i*i + j*j + i*jper i, jnumeri interi non negativi. (Per ogni coppia di numeri interi i, jc'è una coppia di numeri interi non negativi che fornisce lo stesso k)

  • Esiste una serie di kesagoni contigui che formano una tassellatura su una griglia esagonale (vedere le illustrazioni a favore k = 4e a favore k = 7). (A causa di questa proprietà, questi numeri trovano applicazione nelle reti di comunicazione cellulare mobile .)

  • Vedi più caratterizzazioni nella pagina OEIS della sequenza.

La sfida

Dato un numero intero positivo , genera un risultato veritiero se si tratta di un numero loeschiano , o in caso contrario un risultato falso.

Il programma o la funzione dovrebbero gestire (diciamo in meno di un minuto) input fino a 1000, o fino a limiti del tipo di dati.

Codice golf. Vittorie più brevi.

Casi test

I seguenti numeri dovrebbero produrre un risultato veritiero:

1, 4, 7, 12, 13, 108, 109, 192, 516, 999

I seguenti numeri dovrebbero generare un risultato errato:

2, 5, 10, 42, 101, 102, 128, 150, 501, 1000

Correlato (come notato da @PeterTaylor)
Luis Mendo il

nota per l'algoritmo della forza bruta: se si esegue l'iterazione di √k si riduce la complessità dell'algoritmo da O (n²) a O (n), a scapito di alcuni byte c;
Rod

i, j non-negative integerso 9 (i=-3, j=3)- quale è?
Tito

1
@Titus Oh adesso vedo. Per ogni coppia di numeri interi i, j c'è una coppia non negativa che fornisce lo stesso k
Luis Mendo,

Risposte:


17

Gelatina , 11 9 byte

ÆF‘%3,2ḄȦ

Provalo online! o verifica tutti i casi di test .

sfondo

Nei risultati elementari sulla forma quadratica binaria a² + ab + b² , l'autore dimostra il seguente teorema sui numeri di Lösch.

Teorema 16. The necessary and sufficient condition of any non-negative integer to be in the form a² + ab + b² is that, in its prime factorization, all primes other than 3 that are not in the form (6k + 1) have even exponents.

Come indicato nella relativa pagina OEIS , poiché tutti i numeri interi sono congruenti con 0 , 1 o 2 modulo 3, the number 3 is the only prime that is congruent to 0, and all numbers of the form (6k + 1) are congruent to 1, the theorem can be stated alternatively as follows.

Un numero intero non negativo n è un numero di Löschiano se e solo se tutti i fattori primi di n che sono congruenti con 2 modulo 3 hanno anche esponenti.

Come funziona

ÆF‘%3,2ḄȦ  Main link. Argument: n (integer)

ÆF         Yield the prime factorization of n, as prime-exponent pairs.
  ‘        Increment all primes and exponents, turning primes of the form 3k - 2
           into multiples of 3 and odd exponents into multiples of 2.
   %3,2    Reduce all incremented primes/exponents modulo 3/2.
           n is Löschian if and only if this does not result in a [0, 0] pair.
           Due to Jelly's form of vectorization, this yields [3, 2] if n = 1.
       Ḅ   Unbinary; convert each pair from base 2 to integer.
           Note that [x, y] = [0, 0] if and only if 2x + y = 0.
        Ȧ  All; return 1 if the result contains no zeroes, 0 otherwise.

17

Retina , 66 63 45 43 36 byte

^()(\1(?<1>.\1))+(\1(.(?(4).\4)))*$

Nonostante il titolo che dice Retina, questa è solo una semplice regex .NET che accetta rappresentazioni unarie di numeri loeschiani.

Gli input 999 e 1000 richiedono ben meno di un secondo.

Provalo online!(La prima riga abilita una suite di test separata da avanzamento riga e le due successive si occupano della conversione in unario per comodità.)

Spiegazione

La soluzione si basa sulla classificazione secondo cui l'input può essere scritto come i*i + j*(i + j)positivo ie non negativo j(poiché non dobbiamo gestire l'input 0), e questa n*nè solo la somma dei primi nnumeri dispari. Giocare a golf è stato un esercizio interessante nei riferimenti futuri.

Un "riferimento in avanti" è quando si inserisce un backreference all'interno del gruppo a cui fa riferimento. Ovviamente ciò non funziona quando il gruppo viene utilizzato per la prima volta, poiché non c'è ancora nulla di cui fare riferimento, ma se lo si inserisce in un ciclo, il backreference ottiene la cattura dell'iterazione precedente ogni volta. Questo a sua volta, creiamo una cattura più ampia con ogni iterazione. Questo può essere usato per creare modelli molto compatti per cose come numeri triangolari, quadrati e numeri di Fibonacci.

Ad esempio, usando il fatto che i quadrati sono solo somme dei primi nnumeri dispari, possiamo abbinare un input quadrato come questo:

(^.|..\1)+$

Nella prima iterazione, ..\1non può funzionare, perché \1non ha ancora un valore. Quindi iniziamo con ^., catturando un singolo personaggio in gruppo 1. Nelle iterazioni successive, ^.non corrisponde più a causa dell'ancoraggio, ma ora ..\1è valido. Corrisponde a due caratteri in più rispetto alla precedente iterazione e aggiorna la cattura. In questo modo abbiniamo numeri dispari crescenti, ottenendo un quadrato dopo ogni iterazione.

Ora, sfortunatamente, non possiamo usare questa tecnica così com'è. Dopo l'abbinamento i*i, dobbiamo anche ottenere i, in modo da poterlo moltiplicare per j. Un modo semplice (ma lungo) per farlo è quello di utilizzare il fatto che la corrispondenza i*irichiede iiterazioni, in modo che abbiamo catturato le icose in gruppo 1. Ora possiamo usare i gruppi di bilanciamento per estrarre questoi , ma come ho detto è costoso.

Invece, ho trovato un modo diverso di scrivere questa "somma di numeri dispari consecutivi" che ialla fine cede anche in un gruppo di acquisizione. Ovviamente il inumero dispari è giusto 2i-1. Questo ci dà un modo per incrementare il riferimento diretto solo di 1 su ogni iterazione. Questa è questa parte:

^()(\1(?<1>.\1))+

Questo porta ()semplicemente una cattura vuota sul gruppo 1(inizializzazione ia 0). Questo è praticamente equivalente a quello ^.|della soluzione semplice sopra, ma usando| in this case would be a bit trickier.

Quindi abbiamo il ciclo principale (\1(?<1>.\1)). \1corrisponde al precedente i, (?<1>.\1)quindi aggiorna il gruppo 1con i+1. In termini di novità i , abbiamo appena abbinato 2i-1personaggi. Esattamente quello che ci serve.

When we're done, we've matched some square i*i and group 1 still holds i characters.

The second part is closer to the simple square matching I showed above. Let's ignore the backreference to 1 for now:

(.(?(4).\1))*

This is basically the same as (^.|..\4)*, except that we can't make use of ^ because we're not at the start of the string. Instead we make use of a conditional, to match the additional .\1 only when we've already used group 4. But in effect this is exactly the same. This gives us j*j.

The only thing that's missing is the j*i term. We combine this with the j*j by making use of the fact that the j*j computation still takes j iterations. So for each iteration we also advance the cursor by i with \1. We just need to make sure not to write that into group 4, because that would mess with matching consecutive odd numbers. That's how we arrive at the:

(\1(.(?(4).\1)))*

2
The more times I read this, the less I understand. I really want to know that many regex
Javier Diaz

@JavierDiaz There is a series of posts explaining forward references on Stack Overflow, based on Java regex. The examples there are probably a bit simpler.
Martin Ender

13

CJam (16 15 bytes)

{mF{~\3%2=&},!}

Online demo

This is a block (an "anonymous function") which takes input on the stack and leaves 0 or 1 on the stack. It uses the characterisation that a number is Loeschian iff it has no prime factor equal to 2 mod 3 with odd multiplicity.

Thanks to Dennis for a one-byte saving.


Wow, nice characterization!
Luis Mendo

6

Python 2, 56 bytes

lambda n:any(n==i*i%n+i/n*(i/n+i%n)for i in range(2*n*n))

6

Haskell, 42 bytes

f k=or[k==i*i+j*j+i*j|i<-[0..k],j<-[0..i]]

Usage example: f 501 -> False.

Tries all combinations of i from 0 to k and j from 0 to i . or returns True if the equality k==i*i+j*j+i*j holds for at least one of the combinations.

@flawr found a slightly different version with the same byte count:

f k|v<-[0..k]=or[(i+j)^2==k+i*j|i<-v,j<-v]

I didn't know about or, cool=) Perhaps you have an idea how to golf this alternative phrasing: f k|v<-[0..k]=or[(i+j)^2==k+i*j|i<-v,j<-v]?
flawr

@flawr: no, no idea how to golf your version further down. If you don't mind, I'l add it to my answer as an alternative version.
nimi

5

Java 8, 81 bytes

k->{for(int i=0,j;i<=k;i++)for(j=0;j<=k;)if(i*i+j*j+i*j++==k)return 1;return 0;};

simple, naïve implementation. coincidentally same code as C# but uses -> rather than =>.


Three less bytes because you can omit the curly braces and ending ;. DAMN!
TheLethalCoder

@TheLethalCoder I actually can't, I made a mistake - same byte count as C#.
Justin

Makes me feel better anyway :)
TheLethalCoder

This doesn´t seem to test negative i or j.
Titus


4

Jelly, 15 14 13 12 bytes

1 byte thanks to miles.

²S+P
‘ṗ2’Ç€i

Try it online!

Verify the smaller testcases.

A word of advice when testing for large numbers (bigger than 50): don't.

Truthy is a positive number. Falsey is zero.

Explanation

‘ṗ2’Ç€i   main chain, argument: z
‘ṗ2’      generate all pairs of numbers between 0 and z inclusive
    ǀ    apply the helper link to each pair
      i   find the index of z in the result

²S+P   helper link, argument: [x,y] (a pair of numbers)
²      compute [x*x, y*y]
 S     x*x+y*y
  +P   x*x+y*y+x*y

Tied (for) now... :-)
Luis Mendo

Should we exploit Peter's characterization...?
Luis Mendo

@LuisMendo That seems interesting, but it seems that it would be longer
Leaky Nun

I don't think you need to flatten it. Your helper link already maps from tuples to integers.
miles

@miles That's clever, thanks.
Leaky Nun


3

MATL, 14 13 bytes

t:0hU&+HM&*+m

Try it online! Or verify all test cases.

Outputs 1 or 0.

Explanation

t:    % Implicitly input number k. Duplicate. Generate vector [1 2 ...k]
0h    % Concatenate a 0. Gives [1 2 ... k 0]
U     % Square, element-wise. Gives [1 4 ... k^2 0]
&+    % Sum of all pairs from this vector. Gives a (k+1)×(k+1) matrix
HM    % Push [1 2 ... k 0] again
&*    % Product of all pairs from this vector. Gives a (k+1)×(k+1) matrix
+     % Add the two matrices
m     % True if k is a member of the resulting matrix. Implicitly display

Did you just out-golf Jelly?
Leaky Nun

@LeakyNun Let's see how long it lasts. Maybe I'll delay the code explanation a bit :-P
Luis Mendo

Nope. – – – – –
Leaky Nun

Your turn – – –
Leaky Nun

@LeakyNun Aw :-( Now I can add the explanation :-)
Luis Mendo

3

Python, 49 bytes

lambda n:0in[(n-3*i*i+0j)**.5%1for i in range(n)]

Uses the equivalent quadratic form given on OEIS of n == 3*i*i+j*j. Check whether n-3*i*i is a perfect square for any i by taking its square root and checking if it's an integer, i.e. equals 0 modulo 1. Note that Python computes square roots of perfect squares exactly, without floating point error. The +0j makes it a complex number to avoid an error on the square root of a negative.


3

C (gcc), 71 69 bytes

i,j,r;f(n){for(r=i=n+1;i--;)for(j=n;j--;)r*=n!=i*i+j*j+i*j;return!r;}

69 bytes: i,j,r;f(n){for(r=i=n+1;i--;)for(j=n;j--;)r*=n!=i*i+j*j+i*j;return!r;}.
owacoder

This doesn´t seem to test negative i or j.
Titus

@Titus The question dictates non-negative i and j.
orlp

positive k, but not i and j. Take a closer look at the examples.
Titus

@Titus Quoting from the challenge: "k can be expressed as i*i + j*j + i*j for i, j non-negative integers." You take a closer look.
orlp

2

C#, 84 82 81 bytes

k=>{for(int i=0,j;i<=k;++i)for(j=0;j<=k;)if(i*i+j*j+i*j++==k)return 1;return 0;};

A naïve solution. 1 = true, 0 = false


2

VBA, 68 67 bytes

Function L(N):For a=0To N:For b=0To a:L=L+(N=a^2+a*b+b^2):Next b,a

Naive search, starting to slow down slightly for n=1000. Excel recognizes zero return as falsy, all other returns as truthy.

Note that investigation of negative i and j is not needed, since given i>j>=0 :

(-i)2 + (-i)(-j) + (-j)2 = i2 + ij + j2

(the same result as for i and j)

(-i)2 + (-i)j + j2 = i2 - ij + j2

i2 + i(-j) + (-j)2 = i2 - ij + j2

(if one is negative, it doesn't matter which one), and then

(i-j)2 + (i-j)j + j2 = (i2 - 2ij + j2) + (ij - j2) + j2 = i2 - ij + j2

And since both (i-j) and j are non-negative, any generation of Loeschian numbers involving a negative number can be achieved using non-negative numbers.


Saved a byte, Next:Next -> Next b,a thanks to Taylor Scott.


This doesn´t seem to test negative i or j.
Titus

See the first point under "Other equivalent characterizations". Note that all test cases come up correctly. I'll add the mathematical justification to my answer (if I can).
Joffan

Sorry, my fault. Overread that that´s not necessary.
Titus

@Joffan you can condense Next:Next to Next b,a
Taylor Scott

@Joffan looking at your solution again maybe that is because of a missing :End Functionat the end of your solution
Taylor Scott

1

Javascript (using external library - Enumerable) (63 bytes)

k=>_.Range(0,k+1).Any(i=>_.Range(0,k+1).Any(j=>i*i+j*j+i*j==k))

Link to library: https://github.com/mvegh1/Enumerable Code explanation: Create a range of integers from 0 to k (call this the "i" range), and test if any "i" satisfies a certain predicate. That predicate creates a range from 0 to k (call this the "j" range), and tests if any "j" satisfies a certain predicate. That predicate is the loeschian formula

enter image description here


1

Perl 6,  52 51  50 bytes

->\k{?first ->(\i,\j){k==i*i+j*j+i*j},(0..k X 0..k)}
->\k{?grep ->(\i,\j){k==i*i+j*j+i*j},(0..k X 0..k)}

{?grep ->(\i,\j){$_==i*i+j*j+i*j},(0..$_ X 0..$_)}

Explanation:

{
  # Turn the following into a Bool
  # ( Technically not necessary as a list of 1 or more values is truthy )
  ?

  # find all where the code block returns a truthy value
  grep

  # pointy block that takes one value (list of 2 values)
  # and gives each of the values in it a name
  ->
    $ ( \i, \j )
  {
    # return true if the definition matches
    $_ == i*i + j*j + i*j
  },

  # a list of 2 element lists (possible i and j values)
  ( 0..$_ X 0..$_ )
}

Test:

use v6.c;
use Test;

my @true = 0, 1, 4, 7, 12, 13, 108, 109, 192, 516, 999;
my @false = 2, 5, 10, 42, 101, 102, 128, 150, 501, 1000;

plan (@true + @false) * 2;

my &is-loeschian = {?grep ->(\i,\j){$_==i*i+j*j+i*j},(0..$_ X 0..$_)}

for |(@true X True), |(@false X False) -> ( $input, $expected ) {
  my ($result,$seconds) = $input.&time-it;
  is $result, $expected, ~$input;
  cmp-ok $seconds, &[<], 60, "in $seconds seconds"
}

sub time-it ( $input ) {
  my $start = now;
  my $result = $input.&is-loeschian;
  my $finish = now;
  return ( $result, $finish - $start )
}
1..42
ok 1 - 0
ok 2 - in 0.00111763 seconds
ok 3 - 1
ok 4 - in 0.00076766 seconds
...
ok 19 - 516
ok 20 - in 0.19629727 seconds
ok 21 - 999
ok 22 - in 0.1126715 seconds
ok 23 - 2
ok 24 - in 0.0013301 seconds
ok 25 - 5
ok 26 - in 0.00186610 seconds
...
ok 37 - 150
ok 38 - in 0.83877554 seconds
ok 39 - 501
ok 40 - in 9.2968558 seconds
ok 41 - 1000
ok 42 - in 37.31434146 seconds

This doesn´t seem to test negative i or j.
Titus

@Titus the (0..$_ X 0..$_) produces an empty list if $_ is less than 0, so there is no need to check for negative i and j because they will never be negative. Since it is only supposed to produce True for a positive Loeschian number, I don't have to do anything special for the negative case.
Brad Gilbert b2gills

9 = (3*3)+(-3*-3)+(3*-3) is a positive Loeschian with i=3, j=-3; BUT I overread that every Loeschian number has non-negative i and j. So looking for negative numbers is not necessary. So actually we could delete those comments. Sorry for bugging; my fault.
Titus

@Titus modifying the code to {grep ->(\i,\j){$_==i*i+j*j+i*j},(-$_..$_ X -$_..$_)}(9) results in ((-3,0),(-3,3),(0,-3),(0,3),(3,-3),(3,0)). Honestly I probably just adapted it from other answers.
Brad Gilbert b2gills

1

PowerShell v2+, 63 56 55 bytes

param($k)(0..$k|%{0..($i=$_)|%{$i*($i+$_)+$_*$_}})-eq$k

Takes input $k, loops upwards twice (outer loop $i = 0 to $k, inner loop $j = 0 to $i), each iteration generates the result of i*i + j*j + i*j (shortened to i*(i+j) + j*j). Those results are encapsulated in parens, and passed as an array to -eq$k. This acts as a filter to select only elements that equal the input. Outputs a nonzero (the number back) for truthy, or nothing (empty) for falsey. Processes 1000 in about 15 seconds on my machine.

Test Cases

PS C:\Tools\Scripts\golfing> (1,4,7,12,13,108,109,192,516,999|%{.\loeschian-numbers.ps1 $_})-join','
1,4,7,12,13,108,109,192,516,999

PS C:\Tools\Scripts\golfing> (2,5,10,42,101,102,128,150,501,1000|%{.\loeschian-numbers.ps1 $_})-join','

PS C:\Tools\Scripts\golfing>

1

Perl, 54 + 1 (-n flag) = 55 bytes

for$i(0..$_){for$j(0..$_){$i*$i+$j*$j+$i*$j-$_?1:say}}

Needs -n and -M5.010 flags to run :

perl -nE 'for$i(0..$_){for$j(0..$_){$i*$i+$j*$j+$i*$j-$_?1:say}}'

Outputs some stuffs if the number is a Loeschian number, and nothing otherwise.

This implementation is quite boring, so here is another one, for 87 bytes, regex-based, just for the eyes :

perl -pE '$_=(1 x$_)=~/^(.*)(??{$1x(-1+length$1)})(.*)(??{$2x(-1+length$2)})(??{$1x length$2})$/'

Carefull with this one, as the backtracking will use a lot of memory, so don't try to test numbers too big! (especially numbers that aren't Loeschians)


1

Dyalog APL, 19 bytes

⊢∊(∘.(×-⍨2*⍨+)⍨0,⍳)

Checks if k ∊ (i + j)² – ij, for any 0 ≤ i, jk.

     is k
a member of
    ∘. all combinations of
        × i times j
        -⍨ subtracted from
        2*⍨ the square of
        + i plus j
     for all i and j in
    0, zero prepended to
     the integers 1 through k

1000 takes 3.3 seconds on my M540 and even less on TryAPL.


1

Matlab, 53 52 bytes

n=input('');[a b]=ndgrid(0:n);find((a+b).^2-a.*b==n)

Simple search over all possibilities.
Outputs empty array as falsy and a non-empty vector as truthy value.

Considering all-zeros matrix as falsy and not-all-zeros matrix as truthy we can get rid of the find function resulting in 47 46 bytes solution:

n=input('');[a b]=ndgrid(0:n);(a+b).^2-a.*b==n

One byte saved thanks to @flawr


1
(a+b).^2-a.*b==n is shorter.
flawr

1

C, 66 bytes

Call f() with the number to test. The function returns the number of solutions it found.

q,r;f(n){for(r=q=0;q++<n*n;r+=n==q%n*(q%n+q/n)+q/n*q/n);return r;}

Try it on ideone.


1

Mathematica, 44 bytes

MemberQ[(+##)^2-##&@@@0~Range~#~Tuples~2,#]&

Unnamed function taking an integer as input and returning True or False. The command 0~Range~#~Tuples~2 creates all ordered pairs of integers both between 0 and the input #. The function (+##)^2-##& computes the square of the sum of its arguments minus the product of its arguments; when called on two arguments i and j, this is exactly i^2+j^2+ij as desired. So that function is called on all the tuples, and then MemberQ[...,#] checks whether the input is one of the resulting values.


1

ASP, 39 + 4 = 43 bytes

o:-k=I*I+J*J+I*J;I=1..k;J=1..k.:-not o.

Output: the problem is satisfiable iff k is Loeschian.

Answer Set Programming is a logical language, similar to prolog. I use here the Potassco implementation, clingo.

Input is taken from parameters (-ck= is 4 bytes long). Call example:

clingo -ck=999

Output sample:

SATISFIABLE

Tried with 1000:

clingo -ck=1000

Output sample:

UNSATISFIABLE

You can try it in your browser ; unfortunately, this method doesn't handle call flags, so you need to add the line #const k=999 in order to make it work.


Ungolfed & explained code:

v(1..k).  % predicate v(X) holds for any X in [1..k]
o:- k=I*I+J*J+I*J ; v(I) ; v(J).  % o holds if k is Loeschian.
:- not o.  % discard models where o doesn't holds (make problem unsatisfiable)

1

PHP, 70 bytes

for(;$i++<$k=$argv[1];)for($j=$i+1;$j--;)$i*$i+$j*$j+$i*$j-$k?:die(1);

takes input from command line argument; exits with 1 for Loeschian number, with 0 else.
Run with -nr.

breakdown

for(;$i++<$k=$argv[1];)     # loop $i from 1 to $k
    for($j=$i+1;$j--;)      # loop $j from $i to 0
        $i*$i+$j*$j+$i*$j-$k?   # if $i,$j,$k do not satisfy the equation, do nothing
        :die(1);                # else exit with return code 1
                            # implicit: exit with code 0
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.