Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
IT NachrichtenFritzbox-Besitzer sollten diesen Speedtest kennen(24.09.2026 um 06:39 Uhr)
IT NachrichtenApple iOS 27.0.1: Wichtiges Update steht bereit(24.09.2026 um 06:44 Uhr)
Android Tipps & SecurityBYD strebt dichtes Netz an Ladestationen auf Tankstellen-Level an(24.09.2026 um 07:00 Uhr)
IT NachrichtenFritzbox-Besitzer sollten diesen Speedtest kennen(24.09.2026 um 06:39 Uhr)
IT NachrichtenApple iOS 27.0.1: Wichtiges Update steht bereit(24.09.2026 um 06:44 Uhr)
Android Tipps & SecurityBYD strebt dichtes Netz an Ladestationen auf Tankstellen-Level an(24.09.2026 um 07:00 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

The Kth factor of N - an O(sqrt n) algorithm

Introduction Recently I wrote the post Learn Big O Notation once and for all. In that post I go over all of the types of Big O time notation that is available at the Big-O cheatsheet. And I did not think there would be any more time…

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




Introduction



Recently I wrote the post Learn Big O Notation once and for all. In that post I go over all of the types of Big O time notation that is available at the Big-O cheatsheet. And I did not think there would be any more time notations possible outside of those seven.



As if the universe itself was humbling me and mocking my ignorance, I encountered a LeetCode problem with a solution of O(√n) time. Which could be translated to O(N^1/2), if you're crazy.






The problem



You are given two positive integers n and k. A factor of an integer n is defined as an integer i where n % i == 0.



Consider a list of all factors of n sorted in ascending order, return the kth factor in this list or return -1 if n has less than k factors.






The obvious solution



Well, if you're like me your first thought was to go through every number from 1 to n, check if it is a factor, and if it is in the desired k index, return it.



The code looks like this:




def getkthFactorOfN(n, k):
result = 0
for i in range(1, n + 1):
if n % i == 0:
result = result + 1
if result == k:
return i
return -1






This is all fine and dandy, but it is "only" O(n). After all, there is only one loop and it goes up to n + 1.

Every other operation is discarded when considering the time notation.



But, my friend, there's a catch.





Understanding factors



If you think about it, factors are "mirrored" after a certain point.



Take, for example, the number 81. Its factors are [1, 3, 9, 27], where:




  • 1 * 81 = 81

  • 3 * 27 = 81

  • 9 * 9 = 81

  • 27 * 3 = 81

  • 81 * 1 = 81



If you don't count the number 9, The operations are simply repeated and flipped. If you divide n by one of its factors, you get another factor.

Expect the square root of n, where it is itself squared (duh).



Armed with this knowledge, we now know that we don't need to iterate through the loop up to n times (with range(1, n + 1)), but simply up to math.sqrt(n). After that, we've got every factor we need!





The not-so-obvious solution



Now that we have everything we need, we need to transform this loop from 1 -> n to 1 -> sqrt n.



I'll just throw the code here and we'll go over the lines one by one.




def getkthFactorOfN(n, k):
i = 1
factors_asc = []
factors_desc = []
while i * i <= n:
if n % i == 0:
factors_asc.append(i)
if i != n // i:
factors_desc.append(n // i)
i += 1
if k <= len(factors_asc):
return factors_asc[k-1]
k -= len(factors_asc)
if k <= len(factors_desc):
return factors_desc[-k]
return -1






Oof, it's way more complex. Let's break it down:



First, we initialize i = 1. This variable will be used as the "number we're currently at" while searching for factors.



Second, we'll create two arrays: factors_asc and factors_desc. The magic here is that we are going to add factors to factors_asc - they're named like this because they'll be automatically in ascending order.

Whenever we add something to factors_asc, we'll divide n by it and add it to factors_desc. Similar logic here; they'll be conveniently added in descending order.



Then, we begin our loop. Here I've changed it to be while i * i <= n, since we stop when we hit the root of n.



We begin by checking if the current number is a factor (n % i == 0). If so, we can append it to our factors_asc array.



Next, we get the "reverse factor" of i. We can do this by checking if i != n // i, or in other words, if it is not the root. This is because the root must not be duplicated in both arrays. If it isn't, we get the reversed factor by running n // i and appending the result in factors_desc.



After that, we add 1 to i and continue our loop.



After the loop is done, we must have every factorial we need.



We begin by checking if k is in the first half including the root (which can be interpreted as the middle) with if k <= len(factors_asc). If so, get the index from this array (remember: arrays begin at zero!).



If not, we must subtract the amount of factors found from k and check again - with k -= len(factors_asc) and if k <= len(factors_desc).



If k is inside factors_desc, get its value with factors_desk[-k] (from last to first).



If all fails, return -1.






Conclusion



This was a ride to uncover and research. Thank you so much for reading up to this point.



If you want to be more optimized, you can create factors_asc_len and factors_desc_len variables and add +1 every time you append a value to these arrays, so that the method len() doesn't have to be called, since this method is O(n) so it can impact time notation.



Good luck in your studies and until next time!

CTI Threat Relationship Graph2 Knoten / 1 Relationen
CVE / Incident Software MITRE ATT&CK CWE Weakness IoC
SOC Incident Playbook: Vulnerability Remediation & Verification
title: Detect Exploitation - The Kth factor of N - an O(sqrt n) algorithm
id: 05a8194e-ea30-4433-803b-a7aa93ab0df2
status: experimental
description: Automatisch generierte SIEM-Erkennungsregel basierend auf CTI Intelligence
references:
  - https://tsecurity.de/
author: iShareStuff CTI Automated Detection Engine
date: 2026-09-24
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
rule CTI_Threat_Indicator {
    meta:
        author = "iShareStuff CTI Automated Detection Engine"
        date = "2026-09-24"
        description = "YARA Signature for "
    strings:
        $str = "The Kth factor of N - an O(sqr" ascii wide
    condition:
        any of them
}
tsecurity.de Cognitive Threat RAG
Fokus-Vektor:

Kognitive Analyse für identifizierte Bedrohung: Erhöhte Bedrohungslage im Bereich The Kth factor of N - an O(sqrt n) algor.... 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 The Kth factor of N - an O(sqrt n) algorithm

Thematisch verwandte Begriffe: factor, Osqrt, algorithm · 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 ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-96676 | A vulnerability was identified in Fast FAC1900R 20190827_2.0.2. The impa…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
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
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel TTP ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick