Greatest factors, smallest factor

This is my first post so welcome! I just finished working on creating this website from scratch and so far it works, just a little janky in some areas. In this post I'm going to be talking about the integer sequences of the largest and smallest factor of a number. The sequences are recorded but by the time I figured that out I almost finished this post.

For those who don't know, a factor f of a number n is an integer that when n is divided by f, the quotient is equal to an integer (not a decimal). The largest factor of any number n is the largest integer that divides evenly into n. The same applies to the other.

Lets first discuss the largest factor. The basic algorithm to caculate this is as follows ( I call it largestDivisor because Divisor and Factor are kind of interchangeable):

int largestDivisor(int n)
{
    int largestDivisor = 0;
    for (int i = n - 1; i > 1; i--)
    {
        if (n % i == 0)
        {
            largestDivisor = i;
            break;
        }
    }
    return largestDivisor;
}

If a number is prime, then we set its largest divisor is 0. This is because technically the largest divisor of any number is itself and if we were to follow that logic, the squence would not look that interesting. To create a sequence, we can use a for loop to iterate over the positive numbers starting from 1. It gives us the following sequence which is the first 25 numbers:

0, 0, 0, 2, 0, 3, 0, 4, 3, 5, 0, 6, 0, 7, 5, 8, 0, 9, 0, 10, 7, 11, 0, 12

There is no visible pattern seen when just looking at the numbers but when we graph the sequence in a scatter plot, we can see something(s) emerge:

graph of largest factor of an integer

I have no idea why that scatter plot looks like it was created in the 1960s but you can still see the lines of different slopes emerge. If it isn't obvious, the x axis is the number n and the y axis is the largest factor. Just by looking at the first few recognizable lines the slopes in order are:

1/2, 1/3, 1/5, 1/7, 1/11, 1/13, 1/17

The denominators seem to be the prime numbers which is pretty neet. Here is an image of the graph with the slopes as well:

slopes

Now lets look at the smallest factor, the algorithm to calculate this is esentially the same as the largest divisor just changing the for loop from for (int i = n - 1; i > 1; i--) to (int i = 2; i < n; i++). Here is how the graph looks:

graph of smallest factor of an integer

We can sort of see the square root function just by looking at the graph and when we place the function, we get a perfect fit.

graph of smallest factor of an integer with square root function

There is probably a lot more that can be found by looking at these graphs but I'd like to move onto other things. I hope you found this post interesting and learned a little something.

from. Moosa Nasir