Square-free integers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 35: | Line 35: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F SquareFree(_number) |
||
V max = Int(sqrt(_number)) |
V max = Int(sqrt(_number)) |
||
Line 56: | Line 56: | ||
ListSquareFrees(1, 100) |
ListSquareFrees(1, 100) |
||
ListSquareFrees(1000000000000, 1000000000145)</ |
ListSquareFrees(1000000000000, 1000000000145)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 98: | Line 98: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN |
||
# count/show some square free numbers # |
# count/show some square free numbers # |
||
# a number is square free if not divisible by any square and so not divisible # |
# a number is square free if not divisible by any square and so not divisible # |
||
Line 179: | Line 179: | ||
INT sf 1 000 000 := sf 100 000 + count square free( 100 001, 1 000 000 ); |
INT sf 1 000 000 := sf 100 000 + count square free( 100 001, 1 000 000 ); |
||
print( ( "square free numbers between 1 and 1 000 000: ", whole( sf 1 000 000, -6 ), newline ) ) |
print( ( "square free numbers between 1 and 1 000 000: ", whole( sf 1 000 000, -6 ), newline ) ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Square free numbers from 1 to 145 |
<pre>Square free numbers from 1 to 145 |
||
Line 213: | Line 213: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SQUARE-FREE_INTEGERS.AWK |
# syntax: GAWK -f SQUARE-FREE_INTEGERS.AWK |
||
# converted from LUA |
# converted from LUA |
||
Line 252: | Line 252: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 285: | Line 285: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <math.h> |
#include <math.h> |
||
Line 371: | Line 371: | ||
free(sf); |
free(sf); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 411: | Line 411: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <cstdint> |
||
#include <iostream> |
#include <iostream> |
||
#include <string> |
#include <string> |
||
Line 468: | Line 468: | ||
print_square_free_count(1, 1000000); |
print_square_free_count(1, 1000000); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 502: | Line 502: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="d">import std.array; |
||
import std.math; |
import std.math; |
||
import std.stdio; |
import std.stdio; |
||
Line 580: | Line 580: | ||
writefln(" from 1 to %d = %d", to, squareFree(1, to).length); |
writefln(" from 1 to %d = %d", to, squareFree(1, to).length); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Square-free integers from 1 to 145: |
<pre>Square-free integers from 1 to 145: |
||
Line 620: | Line 620: | ||
For instance, the prime factorization of <tt>12</tt> is <code>2 * 2 * 3</code>, or in other words, <code>2<sup>2</sup> * 3</code>. The <tt>2</tt> repeats, so we know <tt>12</tt> isn't square-free. |
For instance, the prime factorization of <tt>12</tt> is <code>2 * 2 * 3</code>, or in other words, <code>2<sup>2</sup> * 3</code>. The <tt>2</tt> repeats, so we know <tt>12</tt> isn't square-free. |
||
< |
<syntaxhighlight lang="factor">USING: formatting grouping io kernel math math.functions |
||
math.primes.factors math.ranges sequences sets ; |
math.primes.factors math.ranges sequences sets ; |
||
IN: rosetta-code.square-free |
IN: rosetta-code.square-free |
||
Line 639: | Line 639: | ||
1 145 10 12 ^ dup 145 + [ sq-free-show ] 2bi@ ! part 1 |
1 145 10 12 ^ dup 145 + [ sq-free-show ] 2bi@ ! part 1 |
||
2 6 [a,b] [ 10 swap ^ ] map [ sq-free-count ] each ! part 2</ |
2 6 [a,b] [ 10 swap ^ ] map [ sq-free-count ] each ! part 2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 677: | Line 677: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: square_free? ( n -- ? ) |
||
dup 4 mod 0= if drop false exit then |
dup 4 mod 0= if drop false exit then |
||
3 |
3 |
||
Line 733: | Line 733: | ||
main |
main |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 773: | Line 773: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' version 06-07-2018 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 861: | Line 861: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 |
<pre> 1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 |
||
Line 895: | Line 895: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 975: | Line 975: | ||
fmt.Printf(" from %d to %d = %d\n", 1, n, len(squareFree(1, n))) |
fmt.Printf(" from %d to %d = %d\n", 1, n, len(squareFree(1, n))) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,016: | Line 1,016: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List.Split (chunksOf) |
||
import Math.NumberTheory.Primes (factorise) |
import Math.NumberTheory.Primes (factorise) |
||
import Text.Printf (printf) |
import Text.Printf (printf) |
||
Line 1,056: | Line 1,056: | ||
printSquareFrees 20 1 145 |
printSquareFrees 20 1 145 |
||
printSquareFrees 5 1000000000000 1000000000145 |
printSquareFrees 5 1000000000000 1000000000145 |
||
printSquareFreeCounts [100, 1000, 10000, 100000, 1000000] 1 1000000</ |
printSquareFreeCounts [100, 1000, 10000, 100000, 1000000] 1 1000000</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,097: | Line 1,097: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">isSqrFree=: (#@~. = #)@q: NB. are there no duplicates in the prime factors of a number? |
||
filter=: adverb def ' #~ u' NB. filter right arg using verb to left |
filter=: adverb def ' #~ u' NB. filter right arg using verb to left |
||
countSqrFree=: +/@:isSqrFree |
countSqrFree=: +/@:isSqrFree |
||
thru=: <. + i.@(+ *)@-~ NB. helper verb</ |
thru=: <. + i.@(+ *)@-~ NB. helper verb</syntaxhighlight> |
||
'''Required Examples:''' |
'''Required Examples:''' |
||
< |
<syntaxhighlight lang="j"> isSqrFree filter 1 thru 145 NB. returns all results, but not all are displayed |
||
1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 51 53 55 57 58 59 61 62 65 66 67 69 70 71 73 74 77 78 79 82 83 85 86 87 89 91 93 94 95 97 101 102 103 105 106 107 109 110 111 113 114 115 118 119 122 123 127 129 130 131... |
1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 33 34 35 37 38 39 41 42 43 46 47 51 53 55 57 58 59 61 62 65 66 67 69 70 71 73 74 77 78 79 82 83 85 86 87 89 91 93 94 95 97 101 102 103 105 106 107 109 110 111 113 114 115 118 119 122 123 127 129 130 131... |
||
100 list isSqrFree filter 1000000000000 thru 1000000000145 NB. ensure that all results are displayed |
100 list isSqrFree filter 1000000000000 thru 1000000000145 NB. ensure that all results are displayed |
||
Line 1,124: | Line 1,124: | ||
608 |
608 |
||
1 countSqrFree@thru&> 10 ^ 2 3 4 5 6 NB. count square free ints for 1 to each of 100, 1000, 10000, 10000, 100000 and 1000000 |
1 countSqrFree@thru&> 10 ^ 2 3 4 5 6 NB. count square free ints for 1 to each of 100, 1000, 10000, 10000, 100000 and 1000000 |
||
61 608 6083 60794 607926</ |
61 608 6083 60794 607926</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="java">import java.util.ArrayList; |
||
import java.util.List; |
import java.util.List; |
||
Line 1,196: | Line 1,196: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,356: | Line 1,356: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">using Primes |
||
const maxrootprime = Int64(floor(sqrt(1000000000145))) |
const maxrootprime = Int64(floor(sqrt(1000000000145))) |
||
Line 1,399: | Line 1,399: | ||
squarefreebetween(1000000000000, 1000000000145) |
squarefreebetween(1000000000000, 1000000000145) |
||
squarefreecount([100, 1000, 10000, 100000], 1000000) |
squarefreecount([100, 1000, 10000, 100000], 1000000) |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
The squarefree numbers between 1 and 145 are: |
The squarefree numbers between 1 and 145 are: |
||
1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 |
1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 |
||
Line 1,435: | Line 1,435: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="scala">// Version 1.2.50 |
||
import kotlin.math.sqrt |
import kotlin.math.sqrt |
||
Line 1,490: | Line 1,490: | ||
j -> println(" from 1 to $j = ${squareFree(1..j).size}") |
j -> println(" from 1 to $j = ${squareFree(1..j).size}") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,532: | Line 1,532: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
This is a naive method, runs in about 1 second on LuaJIT. |
This is a naive method, runs in about 1 second on LuaJIT. |
||
< |
<syntaxhighlight lang="lua">function squareFree (n) |
||
for root = 2, math.sqrt(n) do |
for root = 2, math.sqrt(n) do |
||
if n % (root * root) == 0 then return false end |
if n % (root * root) == 0 then return false end |
||
Line 1,564: | Line 1,564: | ||
{1, 1000000} |
{1, 1000000} |
||
} |
} |
||
for _, example in pairs(testCases) do run(unpack(example)) end</ |
for _, example in pairs(testCases) do run(unpack(example)) end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>From 1 to 145: |
<pre>From 1 to 145: |
||
Line 1,606: | Line 1,606: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
with(NumberTheory): |
with(NumberTheory): |
||
with(ArrayTools): |
with(ArrayTools): |
||
Line 1,648: | Line 1,648: | ||
seq(cat(sfgroups[i], " from 1 to ", 10^(i+1)), i = 1..5); |
seq(cat(sfgroups[i], " from 1 to ", 10^(i+1)), i = 1..5); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
[1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, |
[1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, |
||
Line 1,705: | Line 1,705: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">squareFree[n_Integer] := DeleteCases[Last /@ FactorInteger[n], 1] === {}; |
||
findSquareFree[n__] := Select[Range[n], squareFree]; |
findSquareFree[n__] := Select[Range[n], squareFree]; |
||
findSquareFree[45] |
findSquareFree[45] |
||
findSquareFree[10^9, 10^9 + 145] |
findSquareFree[10^9, 10^9 + 145] |
||
Length[findSquareFree[10^6]]</ |
Length[findSquareFree[10^6]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43} |
<pre>{1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, 31, 33, 34, 35, 37, 38, 39, 41, 42, 43} |
||
Line 1,736: | Line 1,736: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|AWK}} |
{{trans|AWK}} |
||
< |
<syntaxhighlight lang="nanoquery">def is_square_free(n) |
||
if n < 2^2 |
if n < 2^2 |
||
return true |
return true |
||
Line 1,775: | Line 1,775: | ||
square_free(1, 10000, false) |
square_free(1, 10000, false) |
||
square_free(1, 100000, false) |
square_free(1, 100000, false) |
||
square_free(1, 1000000, false)</ |
square_free(1, 1000000, false)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,808: | Line 1,808: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="nim">import math, strutils |
||
Line 1,863: | Line 1,863: | ||
echo "\nNumber of square-free integers:\n" |
echo "\nNumber of square-free integers:\n" |
||
for n in [100, 1_000, 10_000, 100_000, 1_000_000]: |
for n in [100, 1_000, 10_000, 100_000, 1_000_000]: |
||
echo " from $1 to $2 = $3".format(1, n, squareFree(1, n).len)</ |
echo " from $1 to $2 = $3".format(1, n, squareFree(1, n).len)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,903: | Line 1,903: | ||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml"> |
||
let squarefree (number: int) : bool = |
let squarefree (number: int) : bool = |
||
let max = Float.of_int number |> sqrt |> Float.to_int |> (fun x -> x + 2) in |
let max = Float.of_int number |> sqrt |> Float.to_int |> (fun x -> x + 2) in |
||
Line 1,939: | Line 1,939: | ||
print_squarefree_integers (1000000000000, 1000000000146) |
print_squarefree_integers (1000000000000, 1000000000146) |
||
;; |
;; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,989: | Line 1,989: | ||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
As so often, marking/sieving multiples of square of primes to speed things up. |
As so often, marking/sieving multiples of square of primes to speed things up. |
||
< |
<syntaxhighlight lang="pascal">program SquareFree; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE DELPHI} |
{$MODE DELPHI} |
||
Line 2,150: | Line 2,150: | ||
Count_x10; |
Count_x10; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:35ex">Square free numbers from 1 to 145 |
<pre style="height:35ex">Square free numbers from 1 to 145 |
||
Line 2,201: | Line 2,201: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/is_square_free moebius/; |
||
sub square_free_count { |
sub square_free_count { |
||
Line 2,222: | Line 2,222: | ||
my $c = square_free_count(10**$n); |
my $c = square_free_count(10**$n); |
||
print "The number of square-free numbers between 1 and 10^$n (inclusive) is: $c\n"; |
print "The number of square-free numbers between 1 and 10^$n (inclusive) is: $c\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,239: | Line 2,239: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">square_frees</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">square_frees</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">finish</span><span style="color: #0000FF;">)</span> |
||
Line 2,267: | Line 2,267: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" from %,d to %,d = %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" from %,d to %,d = %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,306: | Line 2,306: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<syntaxhighlight lang="python"> |
|||
<lang Python> |
|||
import math |
import math |
||
Line 2,329: | Line 2,329: | ||
ListSquareFrees( 1, 100 ) |
ListSquareFrees( 1, 100 ) |
||
ListSquareFrees( 1000000000000, 1000000000145 ) |
ListSquareFrees( 1000000000000, 1000000000145 ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Output:''' |
'''Output:''' |
||
Line 2,366: | Line 2,366: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (not-square-free-set-for-range range-min (range-max (add1 range-min))) |
(define (not-square-free-set-for-range range-min (range-max (add1 range-min))) |
||
Line 2,408: | Line 2,408: | ||
(count-square-free-numbers 10000) |
(count-square-free-numbers 10000) |
||
(count-square-free-numbers 100000) |
(count-square-free-numbers 100000) |
||
(count-square-free-numbers 1000000))</ |
(count-square-free-numbers 1000000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,448: | Line 2,448: | ||
The prime factoring algorithm is not really the best option for finding long runs of sequential square-free numbers. It works, but is probably better suited for testing arbitrary numbers rather than testing every sequential number from 1 to some limit. If you know that that is going to be your use case, there are faster algorithms. |
The prime factoring algorithm is not really the best option for finding long runs of sequential square-free numbers. It works, but is probably better suited for testing arbitrary numbers rather than testing every sequential number from 1 to some limit. If you know that that is going to be your use case, there are faster algorithms. |
||
<lang |
<syntaxhighlight lang="raku" line># Prime factorization routines |
||
sub prime-factors ( Int $n where * > 0 ) { |
sub prime-factors ( Int $n where * > 0 ) { |
||
return $n if $n.is-prime; |
return $n if $n.is-prime; |
||
Line 2,491: | Line 2,491: | ||
say "\nThe number of square─free numbers between 1 and {$_} (inclusive) is: ", |
say "\nThe number of square─free numbers between 1 and {$_} (inclusive) is: ", |
||
+(1 .. .Int).race.grep: &is-square-free; |
+(1 .. .Int).race.grep: &is-square-free; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,529: | Line 2,529: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program displays square─free numbers (integers > 1) up to a specified limit. */ |
||
numeric digits 20 /*be able to handle larger numbers. */ |
numeric digits 20 /*be able to handle larger numbers. */ |
||
parse arg LO HI . /*obtain optional arguments from the CL*/ |
parse arg LO HI . /*obtain optional arguments from the CL*/ |
||
Line 2,563: | Line 2,563: | ||
if _>=0 then do; x= _; r= r + q; end |
if _>=0 then do; x= _; r= r + q; end |
||
end /*while q>1*/ |
end /*while q>1*/ |
||
return r /*R is the integer square root of X. */</ |
return r /*R is the integer square root of X. */</syntaxhighlight> |
||
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console); not all REXXes have this BIF. |
This REXX program makes use of '''linesize''' REXX program (or BIF) which is used to determine the screen width (or linesize) of the terminal (console); not all REXXes have this BIF. |
||
Line 2,604: | Line 2,604: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require "prime" |
||
class Integer |
class Integer |
||
Line 2,625: | Line 2,625: | ||
puts "#{count} square-frees upto #{n}" if markers.include?(n) |
puts "#{count} square-frees upto #{n}" if markers.include?(n) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 |
<pre>1 2 3 5 6 7 10 11 13 14 15 17 19 21 22 23 26 29 30 31 |
||
Line 2,659: | Line 2,659: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="rust">fn square_free(mut n: usize) -> bool { |
||
if n & 3 == 0 { |
if n & 3 == 0 { |
||
return false; |
return false; |
||
Line 2,719: | Line 2,719: | ||
n *= 10; |
n *= 10; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,751: | Line 2,751: | ||
</pre> |
</pre> |
||
===Rust FP=== |
===Rust FP=== |
||
< |
<syntaxhighlight lang="rust">fn square_free(x: usize) -> bool { |
||
fn iter(x: usize, start: usize, prev: usize) -> bool { |
fn iter(x: usize, start: usize, prev: usize) -> bool { |
||
let limit = (x as f64).sqrt().ceil() as usize; |
let limit = (x as f64).sqrt().ceil() as usize; |
||
Line 2,781: | Line 2,781: | ||
limit, number, duration) |
limit, number, duration) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>square_free numbers between 1 and 145: |
<pre>square_free numbers between 1 and 145: |
||
Line 2,815: | Line 2,815: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
This example uses a brute-force approach to check whether a number is square-free, and builds a lazily evaluated list of all square-free numbers with a simple filter. To get the large square-free numbers, it avoids computing the beginning of the list by starting the list at a given number. |
This example uses a brute-force approach to check whether a number is square-free, and builds a lazily evaluated list of all square-free numbers with a simple filter. To get the large square-free numbers, it avoids computing the beginning of the list by starting the list at a given number. |
||
< |
<syntaxhighlight lang="scala">import spire.math.SafeLong |
||
import spire.implicits._ |
import spire.implicits._ |
||
Line 2,854: | Line 2,854: | ||
fHelper(Vector[String](), formatted) |
fHelper(Vector[String](), formatted) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,895: | Line 2,895: | ||
In Sidef, the functions ''is_square_free(n)'' and ''square_free_count(min, max)'' are built-in. However, we can very easily reimplement them in Sidef code, as fast integer factorization methods are also available in the language. |
In Sidef, the functions ''is_square_free(n)'' and ''square_free_count(min, max)'' are built-in. However, we can very easily reimplement them in Sidef code, as fast integer factorization methods are also available in the language. |
||
< |
<syntaxhighlight lang="ruby">func is_square_free(n) { |
||
n.abs! if (n < 0) |
n.abs! if (n < 0) |
||
Line 2,928: | Line 2,928: | ||
var c = square_free_count(10**n) |
var c = square_free_count(10**n) |
||
say "The number of square─free numbers between 1 and 10^#{n} (inclusive) is: #{c}" |
say "The number of square─free numbers between 1 and 10^#{n} (inclusive) is: #{c}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,971: | Line 2,971: | ||
{{libheader|AttaSwift BigInt}} |
{{libheader|AttaSwift BigInt}} |
||
<lang>import BigInt |
<syntaxhighlight lang="text">import BigInt |
||
import Foundation |
import Foundation |
||
Line 3,033: | Line 3,033: | ||
break |
break |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,046: | Line 3,046: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc isSquarefree {n} { |
||
for {set d 2} {($d * $d) <= $n} {set d [expr {($d+1)|1}]} { |
for {set d 2} {($d * $d) <= $n} {set d [expr {($d+1)|1}]} { |
||
if {0 == ($n % $d)} { |
if {0 == ($n % $d)} { |
||
Line 3,103: | Line 3,103: | ||
showCount 1 $H |
showCount 1 $H |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>Square-free integers in range 1..145 are: |
<pre>Square-free integers in range 1..145 are: |
||
Line 3,138: | Line 3,138: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Function Sieve(limit As Long) As List(Of Long) |
Function Sieve(limit As Long) As List(Of Long) |
||
Line 3,224: | Line 3,224: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Square-free integers from 1 to 145: |
<pre>Square-free integers from 1 to 145: |
||
Line 3,262: | Line 3,262: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
var isSquareFree = Fn.new { |n| |
var isSquareFree = Fn.new { |n| |
||
Line 3,295: | Line 3,295: | ||
} |
} |
||
System.print(count) |
System.print(count) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,335: | Line 3,335: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">const Limit=1 + (1e12 + 145).sqrt(); // 1000001 because it fits this task |
||
var [const] |
var [const] |
||
BI=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library |
BI=Import.lib("zklBigNum"), // GNU Multiple Precision Arithmetic Library |
||
Line 3,355: | Line 3,355: | ||
} |
} |
||
return(cnt,sink.close()); |
return(cnt,sink.close()); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">println("Square-free integers from 1 to 145:"); |
||
squareFree(1,145,True)[1].pump(Console.println, |
squareFree(1,145,True)[1].pump(Console.println, |
||
T(Void.Read,14,False),fcn{ vm.arglist.apply("%4d ".fmt).concat() }); |
T(Void.Read,14,False),fcn{ vm.arglist.apply("%4d ".fmt).concat() }); |
||
Line 3,362: | Line 3,362: | ||
println("\nSquare-free integers from 1000000000000 to 1000000000145:"); |
println("\nSquare-free integers from 1000000000000 to 1000000000145:"); |
||
squareFree(1000000000000,1000000000145,True)[1].pump(Console.println, |
squareFree(1000000000000,1000000000145,True)[1].pump(Console.println, |
||
T(Void.Read,4,False),fcn{ vm.arglist.concat(" ") });</ |
T(Void.Read,4,False),fcn{ vm.arglist.concat(" ") });</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:35ex"> |
<pre style="height:35ex"> |
||
Line 3,394: | Line 3,394: | ||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="zkl">n:=100; do(5){ |
||
squareFree(1,n)[0]: |
squareFree(1,n)[0]: |
||
println("%,9d square-free integers from 1 to %,d".fmt(_,n)); |
println("%,9d square-free integers from 1 to %,d".fmt(_,n)); |
||
n*=10; |
n*=10; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |