How to resolve the algorithm Ascending primes step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ascending primes step by step in the FreeBASIC programming language

Table of Contents

Problem Statement

Generate and show all primes with strictly ascending decimal digits. Aside: Try solving without peeking at existing solutions. I had a weird idea for generating a prime sieve faster, which needless to say didn't pan out. The solution may be p(r)etty trivial but generating them quickly is at least mildly interesting. Tip: filtering all 7,027,260 primes below 123,456,789 probably won't kill you, but there is at least one significantly better and much faster way, needing a mere 511 odd/prime tests.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ascending primes step by step in the FreeBASIC programming language

Source code in the freebasic programming language

#include "isprime.bas"
#include "sort.bas"

Dim As Integer i, n, tmp, num, cant = 0
Dim Shared As Integer matriz(512)
For i = 0 To Ubound(matriz)-1
    n = 0
    tmp = i
    num = 1
    While tmp
        If tmp And 1 Then n = n * 10 + num
        tmp Shr= 1
        num += 1
    Wend
    matriz(i)= n
Next i

Sort(matriz())

For i = 1 To Ubound(matriz)-1     'skip empty set
    n = matriz(i)
    If isPrime(n) Then
        Print Using "#########"; n;
        cant += 1
        If cant Mod 10 = 0 Then Print
    End If
Next i
Print Using !"\nThere are & ascending primes."; cant

Sleep

  

You may also check:How to resolve the algorithm Variable size/Get step by step in the Delphi programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the RapidQ programming language
You may also check:How to resolve the algorithm The Twelve Days of Christmas step by step in the COBOL programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the Phix programming language
You may also check:How to resolve the algorithm System time step by step in the Prolog programming language