🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsAmazon Prime Big Deal Days: October 6 to October 7, 2026(15.09.2026 um 11:36 Uhr)
🪟 Windows TippsSpotify(15.09.2026 um 11:30 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🕵️ SicherheitslückenBurn Out, Or Fade Away(14.09.2026 um 14:25 Uhr)
🪟 Windows TippsAmazon Prime Big Deal Days: October 6 to October 7, 2026(15.09.2026 um 11:36 Uhr)
🪟 Windows TippsSpotify(15.09.2026 um 11:30 Uhr)
🪟 Windows TippsKB5129194 Windows 11 26H1 Out of Band Update - Deskmodder.de(14.09.2026 um 19:25 Uhr)

🔧 Programmierung 🕛 vor 2 Jahren 4 Min Lesezeit
0

PWC 246 Random use of algebra

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht
📺
dev.to

It's December, and that means neglecting our responsibilities with





CODE
6 out of 49 is a German lottery.
Write a script that outputs six unique
random integers from the range 1 to 49.






Let's be random. 6 out of 49.





CODE
You are given an array @a of five integers.
Write a script to decide whether the given
integers form a linear recurrence of second
order with integer factors.

A linear recurrence of second order has the form
a[n] = p * a[n-2] + q * a[n-1] with n > 1
where p and q must be integers.







  • Example 1: 1,1,2,3,5 is a second-order linear recurrence because a[n] = a[n-2] + a[n-1] (it's the beginning of the Fibonacci sequence of course).

  • Example 2: 2,4,2,5,7 can't be a linear recurrence because it starts with multiples of even numbers, and therefore no combination of them will yield an odd number.

  • Example 3: 4,1,2,-3,8 is a sequence that can be created with the recurrence a[n] = a[n-2] - 2 * a[n-1]



It's time to smugly take revenge on the bozos in algebra class who used to whine, "But when will we ever use this in real life?" Ha! The time has come, and as far as I can tell, this is real life. Or a damn good simulation.



To be a second-order recurrence relation, we have to find p and q that solve the pair of equations




  • a[0]*p + a[1]*q = a[2]

  • a[1]*p + a[2]*q = a[3]



and using those values of p and q, we also have to verify that a[4] can be generated from a[2]*p + a[3]*q = a[4]



From the first equation, we will get

p = (a[2] - a[1]*q)/a[0]

Substituting that into the second equation, we will get

q = (a[1]*a[2] - a[0]*a[3]) / (a[1]*a[1] - a[0]*a[2] )



So, given a sequence of at least 4 values, we can determine q, and from that we can determine p. Both have to be integers, per the problem statement. It's also possible that one of those denominators is zero, meaning that the recurrence can't be satisfied.



We could golf this down to some pretty cryptic code, but let's throw a bone to readability this week. First step, let's define the function that produces q:




CODE
sub fq($a0, $a1, $a2, $a3)
{
my $denom = $a1*$a1 - $a0*$a2;
return undef if $denom == 0;
my $q = ($a1*$a2 - $a0*$a3)/$denom;
return $q;
}






Amusing aside: I originally wanted to call this function q, but it gave me weird results. Do you see it? Of course: q is the built-in single-quote operator. A function q can be defined, but it can't be called because it won't override the operator.



Second step, let's define the function that produces p.




CODE
sub fp($q, $a0, $a1, $a2)
{
return undef if $a0 == 0;
my $p = ($a2 - $a1 * $q) / $a0;
return $p;
}






And if we can find p and q, then we can write the code to check for a linear recurrence:




CODE
sub isl2(@a)
{
my $q = fq( @a[0..3] );
return false unless defined $q && int($q) == $q;

my $p = fp($q, @a[0..2]);
return false unless defined $p && int($p) == $p;

# Must also be true for remaing values of @a
for my $i ( 4 .. $#a )
{
my $nexta = $p * $a[$i-2] + $q * $a[$i-1];
if ( $a[$i] != $nexta )
{
return false;
}
}
return true;
}






Where did true and false come from? Since version 5.36, these booleans are available as built-ins, and this was a natural place to use them. They're still experimental, so using them requires




CODE
use builtin qw/true false/; no warnings "experimental::builtin"


Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
How to evaluate LLMs before production
1 Quelle
Profiles now show your highest achievement badge tier
1 Quelle
Nach dem OpenAI-Hack: 5 Software-Typen, die du unbedingt prüfen solltest
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten PWC 246 Random use of algebra

Thematisch verwandte Begriffe: Random, algebra · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...