Zum Hauptinhalt springen
Echtzeit-Radar & Feeds
Alle RSS Feeds ➔
👥 Community & Social
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Windows Tipps & SecurityGrafikkarte vor Überhitzung schützen: So geht’s(25.09.2026 um 08:00 Uhr)
••••••••••
Intelligence View
⚡ tsecurity.de Intelligence

2523. Closest Prime Numbers in Range

2523. Closest Prime Numbers in Range Difficulty: Medium Topics: Math, Number Theory Given two positive integers left and right, find the two integers num1 and num2 such that: left <= num1 < num2 <= right . Both num1 and num2…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

2523. Closest Prime Numbers in Range



Difficulty: Medium



Topics: Math, Number Theory



Given two positive integers left and right, find the two integers num1 and num2 such that:





  • left <= num1 < num2 <= right .

  • Both num1 and num2 are prime numbers1.


  • num2 - num1 is the minimum amongst all other pairs satisfying the above conditions.



Return the positive integer array ans = [num1, num2]. If there are multiple pairs satisfying these conditions, return the one with the smallest num1 value. If no such numbers exist, return [-1, -1].



Example 1:





  • Input: left = 10, right = 19


  • Output: [11,13]


  • Explanation: The prime numbers between 10 and 19 are 11, 13, 17, and 19.


    • The closest gap between any pair is 2, which can be achieved by [11,13] or [17,19].

    • Since 11 is smaller than 17, we return the first pair.








Example 2:





  • Input: left = 4, right = 6


  • Output: [-1,-1]


  • Explanation: There exists only one prime number in the given range, so the conditions cannot be satisfied.



Constraints:




  • 1 <= left <= right <= 106



Hint:




  1. Use Sieve of Eratosthenes to mark numbers that are primes.

  2. Iterate from right to left and find pair with the minimum distance between marked numbers.



Solution:



We need to find the two closest prime numbers within a given range [left, right]. If there are multiple pairs with the same minimum difference, we return the pair with the smallest starting prime. If there are fewer than two primes in the range, we return [-1, -1].






Approach





  1. Generate Primes Efficiently: Use the Sieve of Eratosthenes algorithm to generate all prime numbers up to the given right value. This algorithm efficiently marks non-prime numbers, allowing us to quickly identify primes within the range.


  2. Collect Primes in Range: Extract all prime numbers from the generated sieve that lie within the range [left, right].


  3. Find Closest Primes: Iterate through the collected primes and compute the differences between consecutive primes. Track the minimum difference and the corresponding pair of primes.



Let's implement this solution in PHP: 2523. Closest Prime Numbers in Range




<?php
/**
* @param Integer $left
* @param Integer $right
* @return Integer[]
*/

function closestPrimes($left, $right) {
...
...
...
/**
* go to ./solution.php
*/

}

// Example test cases
echo json_encode(closestPrimes(10, 19)); // Output: [11,13]
echo "\n";
echo json_encode(closestPrimes(4, 6)); // Output: [-1,-1]
?>









Explanation:





  1. Sieve of Eratosthenes: This algorithm efficiently marks non-prime numbers by iterating through each number starting from 2 and marking its multiples. This ensures that by the end of the process, only prime numbers remain marked as true in the sieve array.


  2. Prime Collection: After generating the sieve, we collect all primes within the specified range [left, right]. This step filters out primes that are outside the given bounds.


  3. Finding Closest Primes: By iterating through the collected primes and comparing consecutive pairs, we determine the pair with the smallest difference. The first occurrence of the minimum difference is chosen to ensure the smallest starting prime in case of ties.



This approach ensures that we efficiently generate primes and find the closest pair using optimal time and space complexity.



Contact Links



If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!



If you want more helpful content like this, feel free to follow me:











  1. Prime A prime number is a natural number greater than 1 with only two factors, 1 and itself. ↩




1. Sofort-Triage & Abwehrmaßnahmen

SOC Incident Playbook: Vulnerability Remediation & Verification
Syntax validiert (0 Fehler)
title: Detect Exploitation - 2523. Closest Prime Numbers in Range
id: f6f5b455-24b3-40c9-9010-430bd1e85007
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-26
logsource:
  category: network_connection
  product: any
detection:
  selection:
      CommandLine|contains:
        - 'exploit'
  condition: selection
falsepositives:
  - Legitime administrative Zugriffe oder Penetrationstests
level: high
tags:
  - attack.initial_access
Syntax validiert (0 Fehler)
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-26"
        description = "YARA Signature for "
    strings:
        $str = "2523. Closest Prime Numbers in" ascii wide
    condition:
        any of them
}
Syntax validiert (0 Fehler)
index=security sourcetype IN ("cisco:asa", "pan:traffic", "zeek_conn", "suricata", "WinEventLog:Security")
("2523 Closest Prime Numbers in Range")
| stats count earliest(_time) as first_seen latest(_time) as last_seen by src_ip, dest_ip, dest_host, signature
| eval first_seen=strftime(first_seen, "%Y-%m-%d %H:%M:%S"), last_seen=strftime(last_seen, "%Y-%m-%d %H:%M:%S")
| sort - count
Syntax validiert (0 Fehler)
message: "*2523 Closest Prime Numbers in Range*"
Syntax validiert (0 Fehler)
CommonSecurityLog
| where Message has "2523 Closest Prime Numbers in Range"
| summarize EventCount = count(), FirstSeen = min(TimeGenerated), LastSeen = max(TimeGenerated) by SourceIP, DestinationIP, DestinationPort, Activity
| extend DetectionRule = "iShareStuff-CTI-Compiled"
| sort by EventCount desc

2. Cyber Threat Intelligence & Forensik

🎯
MITRE ATT&CK Matrix Navigator 14 Taktiken
Reconnaissance
-
Resource Development
-
Initial Access
Execution
Persistence
-
Privilege Escalation
Defense Evasion
Credential Access
-
Discovery
-
Lateral Movement
-
Collection
-
Command and Control
Exfiltration
-
Impact
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich 2523. Closest Prime Numbers in Range.... Basierend auf 368k Vektor-Korrelationen werden sofortige Isolationsmaßnahmen für betroffene Endpunkte empfohlen.

🛡️ Angriffsfläche & Exposure

Netzwerk/Remote-Zugriff ohne Vorauthentifizierung möglich.

⚡ Empfohlene Sofortmaßnahmen
  • 1. Perimeter-Inspektion: Relevante Portfreigaben und exponierte Endpunkte unverzüglich scannen.
  • 2. Patch-Applikation: Hersteller-Hotfix einspielen oder betroffene Daemons in isolierte DMZ-Segmente überführen.
  • 3. Telemetrie & EDR-Alerts: Prozessaufrufe und Child-Processes auf anomale Shell-Spawns überwachen.
🔗 Semantisch verwandte Zero-Days MariaDB 11.7 VEC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 2523. Closest Prime Numbers in Range

Thematisch verwandte Begriffe: 2523, Closest, Prime, Numbers · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-100620 | Capgo CLI (npm package @capgo/cli) through 7.98.2 is affected by an ove…
Advisory →
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag