Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 67 / 68

Finding the prime factors

This lesson covers prime factorisation in VB.NET, with the aim of understanding a number-theory algorithm built on loops.

Prime factorisation breaks a number down into a product of primes. Testing divisibility from the smallest prime upwards, and dividing for as long as it divides, finds every prime factor.

The sample code starts at d = 2 and, while n divides by d, records the factor with factors.Add(d) and keeps dividing, increasing d by one when it no longer divides — repeating until n reaches 1.

A common early stumble is the nested loop structure. The outer loop increases the divisor d while the inner one keeps dividing for as long as it can — that two-stage arrangement is the key to understanding it.

Prime factorisation underpins cryptography such as RSA, which makes it a worthwhile way into number-theory algorithms.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)