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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ascending primes step by step in the FutureBasic 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 FutureBasic programming language

Source code in the futurebasic programming language

local fn IsPrime( n as NSUInteger ) as BOOL
  BOOL       isPrime = YES
  NSUInteger i
  
  if n < 2        then exit fn = NO
  if n = 2        then exit fn = YES
  if n mod 2 == 0 then exit fn = NO
  for i = 3 to int(n^.5) step 2
    if n mod i == 0 then exit fn = NO
  next
end fn = isPrime

void local fn AscendingPrimes( limit as long )
  long i, n, mask, num, count = 0
  
  for i = 0 to limit -1
    n = 0 : mask = i : num = 1
    while ( mask )
      if mask & 1 then n = n * 10 + num
      mask = mask >> 1
      num++
    wend
    mda(i) = n
  next
  
  mda_sort @"compare:"
  
  for i = 1 to mda_count (0) - 1
    n = mda_integer(i)
    if ( fn IsPrime( n ) )
      printf @"%10ld\b", n
      count++
      if count mod 10 == 0 then print
    end if
  next
  printf @"\n\tThere are %ld ascending primes.", count
end fn

window 1, @"Ascending Primes", ( 0, 0, 780, 230 )
print

CFTimeInterval t
t = fn CACurrentMediaTime
fn AscendingPrimes( 512 )
printf @"\n\tCompute time: %.3f ms\n",(fn CACurrentMediaTime-t)*1000

HandleEvents

  

You may also check:How to resolve the algorithm Repeat a string step by step in the Arturo programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Fantom programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the K programming language
You may also check:How to resolve the algorithm Bitwise IO step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Erlang programming language