Task: Square-free integers


C++: #include <cstdint>
#include <iostream>
#include <string>

using integer = uint64_t;

bool square_free(integer n) {
    if (n % 4 == 0)
        return false;
    for (integer p = 3; p * p <= n; p += 2) {
        integer count = 0;
        for (; n % p == 0; n /= p) {
            if (++count > 1)
                return false;
        }
    }
    return true;
}

void print_square_free_numbers(integer from, integer to) {
    std::cout << "Square-free numbers between " << from
        << " and " << to << ":\n";
    std::string line;
    for (integer i = from; i <= to; ++i) {
        if (square_free(i)) {
            if (!line.empty())
                line += ' ';
            line += std::to_string(i);
            if (line.size() >= 80) {
                std::cout << line << '\n';
                line.clear();
            }
        }
    }
    if (!line.empty())
        std::cout << line << '\n';
}

void print_square_free_count(integer from, integer to) {
    integer count = 0;
    for (integer i = from; i <= to; ++i) {
        if (square_free(i))
            ++count;
    }
    std::cout << "Number of square-free numbers between "
        << from << " and " << to << ": " << count << '\n';
}

int main() {
    print_square_free_numbers(1, 145);
    print_square_free_numbers(1000000000000LL, 1000000000145LL);
    print_square_free_count(1, 100);
    print_square_free_count(1, 1000);
    print_square_free_count(1, 10000);
    print_square_free_count(1, 100000);
    print_square_free_count(1, 1000000);
    return 0;
}







VB: Module Module1

    Function Sieve(limit As Long) As List(Of Long)
        Dim primes As New List(Of Long) From {2}
        Dim c(limit + 1) As Boolean
        Dim p = 3L
        While True
            Dim p2 = p * p
            If p2 > limit Then
                Exit While
            End If
            For i = p2 To limit Step 2 * p
                c(i) = True
            Next
            While True
                p += 2
                If Not c(p) Then
                    Exit While
                End If
            End While
        End While
        For i = 3 To limit Step 2
            If Not c(i) Then
                primes.Add(i)
            End If
        Next
        Return primes
    End Function

    Function SquareFree(from As Long, to_ As Long) As List(Of Long)
        Dim limit = CType(Math.Sqrt(to_), Long)
        Dim primes = Sieve(limit)
        Dim results As New List(Of Long)

        Dim i = from
        While i <= to_
            For Each p In primes
                Dim p2 = p * p
                If p2 > i Then
                    Exit For
                End If
                If (i Mod p2) = 0 Then
                    i += 1
                    Continue While
                End If
            Next
            results.Add(i)
            i += 1
        End While

        Return results
    End Function

    ReadOnly TRILLION As Long = 1_000_000_000_000

    Sub Main()
        Console.WriteLine("Square-free integers from 1 to 145:")
        Dim sf = SquareFree(1, 145)
        For index = 0 To sf.Count - 1
            Dim v = sf(index)
            If index > 1 AndAlso (index Mod 20) = 0 Then
                Console.WriteLine()
            End If
            Console.Write("{0,4}", v)
        Next
        Console.WriteLine()
        Console.WriteLine()

        Console.WriteLine("Square-free integers from {0} to {1}:", TRILLION, TRILLION + 145)
        sf = SquareFree(TRILLION, TRILLION + 145)
        For index = 0 To sf.Count - 1
            Dim v = sf(index)
            If index > 1 AndAlso (index Mod 5) = 0 Then
                Console.WriteLine()
            End If
            Console.Write("{0,14}", v)
        Next
        Console.WriteLine()
        Console.WriteLine()

        Console.WriteLine("Number of square-free integers:")
        For Each to_ In {100, 1_000, 10_000, 100_000, 1_000_000}
            Console.WriteLine("   from 1 to {0} = {1}", to_, SquareFree(1, to_).Count)
        Next
    End Sub

End Module