Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 67 / 68

Finding the prime factors

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

Prime factorisation expresses a number as a product of primes. Try dividing from 2 upwards, and whenever a number divides, keep dividing by it.

The sample code starts at d = 2 and, while n divides by d, records the factor with push!(factors, d) and keeps dividing with n = div(n, d), 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.

It is an important idea spanning mathematics and computer science, and underpins the security of cryptography such as RSA.

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

๐Ÿงช This site can't compile or run Julia 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)