How to resolve the algorithm Square-free integers step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Square-free integers step by step in the zkl programming language

Table of Contents

Problem Statement

Write a function to test if a number is   square-free.

A   square-free   is an integer which is divisible by no perfect square other than   1   (unity). For this task, only positive square-free numbers will be used.

Show here (on this page) all square-free integers (in a horizontal format) that are between:

(One trillion = 1,000,000,000,000)

Show here (on this page) the count of square-free integers from:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Square-free integers step by step in the zkl programming language

Source code in the zkl programming language

const Limit=1 + (1e12 + 145).sqrt();	// 1000001 because it fits this task
var [const] 
   BI=Import.lib("zklBigNum"),    // GNU Multiple Precision Arithmetic Library
   primes=List.createLong(Limit); // one big allocate (vs lots of allocs)

// GMP provide nice way to generate primes, nextPrime is in-place
p:=BI(0); while(p

fcn squareFree(start,end,save=False){ //-->(cnt,list|n)
   sink := Sink(if(save) List else Void);  // Sink(Void) is one item sink
   cnt, numPrimes := 0, (end - start).toFloat().sqrt().toInt() - 1;
   foreach n in ([start..end]){
      foreach j in ([0..numPrimes]){
         p,p2 := primes[j], p*p;
	 if(p2>n) break;
	 if(n%p2==0) continue(2);  // -->foreach n
      }
      sink.write(n); cnt+=1
   }
   return(cnt,sink.close());
}

println("Square-free integers from 1 to 145:");
squareFree(1,145,True)[1].pump(Console.println,
   T(Void.Read,14,False),fcn{ vm.arglist.apply("%4d ".fmt).concat() });

println("\nSquare-free integers from 1000000000000 to 1000000000145:");
squareFree(1000000000000,1000000000145,True)[1].pump(Console.println,
   T(Void.Read,4,False),fcn{ vm.arglist.concat(" ") });

n:=100; do(5){ 
   squareFree(1,n)[0]:
      println("%,9d square-free integers from 1 to %,d".fmt(_,n));
   n*=10;
}

  

You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the Wren programming language
You may also check:How to resolve the algorithm Multi-dimensional array step by step in the J programming language
You may also check:How to resolve the algorithm Permutations step by step in the REXX programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Sorting algorithms/Pancake sort step by step in the Pascal programming language