Square form factorization: Difference between revisions

m
syntax highlighting fixup automation
(J draft)
m (syntax highlighting fixup automation)
Line 52:
===Based on Wp===
From the Wikipedia entry for Shanks's square forms factorization [[//en.wikipedia.org/wiki/Shanks%27s_square_forms_factorization.]]
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
 
Line 162:
}
}
</langsyntaxhighlight>{{out}}
<pre>
Integer 2501 has factors 61 and 41
Line 196:
===Classical heuristic===
See [[Talk:Square_Form_Factorization|Discussion]].
<langsyntaxhighlight lang="c">//SquFoF: minimalistic version without queue.
//Classical heuristic. Tested: tcc 0.9.27
#include <math.h>
Line 334:
else printf("f = %llu N/f = %llu\n\n", f, N/f);
}
}</langsyntaxhighlight>
 
{{out|Showing problem cases only}}
Line 363:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' ***********************************************
'subject: Shanks's square form factorization:
' ambiguous forms of discriminant 4N
Line 685:
 
print "total time:"; csng(timer - tim); " s"
end</langsyntaxhighlight>
 
{{out|Examples}}
Line 783:
 
Rather than juggling with big.Int, I've just allowed the two 'awkward' cases to fail.
<langsyntaxhighlight lang="go">package main
 
import (
Line 909:
fmt.Printf("%-20d %-10d %s\n", N, fact, quot)
}
}</langsyntaxhighlight>
 
{{out}}
Line 947:
=={{header|J}}==
{{eff note|J|q:}}
J does not have an unsigned fixed width integer type, which is one of the reasons that (in J) this algorithm is less optimal than advertised:<langsyntaxhighlight Jlang="j">sqff=: {{
s=. <.%:y
if. y=*:s do. s return. end.
Line 975:
end.
1
}}</langsyntaxhighlight>
 
Task examples:<langsyntaxhighlight Jlang="j"> task ''
2501: 61 * 41
12851: 71 * 181
Line 1,005:
1152921505680588799: 139001459 * 8294312261
1537228672809128917 was not factored
4611686018427387877 was not factored</langsyntaxhighlight> where <syntaxhighlight lang J="j">task=: {{
for_num. nums do.
factor=. x:sqff num
Line 1,043:
1537228672809128917
4611686018427387877x
}}-.LF</langsyntaxhighlight>
 
=={{header|jq}}==
Line 1,056:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq">def gcd(a; b):
# subfunction expects [a,b] as input
# i.e. a ~ .[0] and b ~ .[1]
Line 1,086:
then irt
else "isqrt requires a non-negative integer for accuracy" | error
end ;</langsyntaxhighlight>
'''The Tasks'''
<langsyntaxhighlight lang="jq">def multipliers:
[
1, 3, 5, 7, 11, 3*5, 3*7, 3*11, 5*7, 5*11, 7*11, 3*5*7, 3*5*11, 3*7*11, 5*7*11, 3*5*7*11
Line 1,194:
| [$N, $fact, $quot]
end
)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,231:
=={{header|Julia}}==
Modified from Wikipedia's article at [[https://en.wikipedia.org/wiki/Shanks%27s_square_forms_factorization]]
<langsyntaxhighlight lang="julia">function square_form_factor(n::T)::T where T <: Integer
multiplier = T.([1, 3, 5, 7, 11, 3*5, 3*7, 3*11, 5*7, 5*11, 7*11, 3*5*7, 3*5*11, 3*7*11, 5*7*11, 3*5*7*11])
s = T(round(sqrt(n)))
Line 1,287:
println(factr == 0 ? "fail" : n ÷ factr)
end
</langsyntaxhighlight>{{out}}
<pre>
Integer Factor Quotient
Line 1,324:
=={{header|Nim}}==
{{trans|Phix}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
const M = [uint64 1, 3, 5, 7, 11]
Line 1,419:
let f = squfof(n)
let res = if f == 1: "fail" else: &"{f:<10} {n div f}"
echo &"{n:<22} {res}"</langsyntaxhighlight>
 
{{out}}
Line 1,456:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,514:
elsif ($v == 1) { say 'The number ' . $data . ' is a prime.' }
else { say "$data = " . join ' * ', sort {$a <=> $b} $v, int $data/int($v) }
}</langsyntaxhighlight>
{{out}}
<pre>11111 = 41 * 271
Line 1,548:
=={{header|Phix}}==
{{trans|C|<small>(Classical heuristic - fixes the two incorrectly failing cases of the previous version)</small>}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--requires(64) -- (decided to limit 32-bit explicitly instead)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">MxN</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">power<span style="color: #0000FF;">(<span style="color: #000000;">2<span style="color: #0000FF;">,<span style="color: #008080;">iff<span style="color: #0000FF;">(<span style="color: #7060A8;">machine_bits<span style="color: #0000FF;">(<span style="color: #0000FF;">)<span style="color: #0000FF;">=<span style="color: #000000;">32<span style="color: #0000FF;">?<span style="color: #000000;">53<span style="color: #0000FF;">:<span style="color: #000000;">63<span style="color: #0000FF;">)<span style="color: #0000FF;">)<span style="color: #0000FF;">,</span>
Line 1,635:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for
<!--</langsyntaxhighlight>-->
{{out}}
<small>(on 64-bit, whereas the last 10 entries, ie 11111111111111111 on, are simply omitted on 32-bit)</small>
Line 1,680:
===A just for fun snail ..===
References: [https://en.wikipedia.org/wiki/Shanks%27s_square_forms_factorization#Algorithm Algorithm], [https://en.wikipedia.org/wiki/Shanks%27s_square_forms_factorization#Example_implementations C program example] from the WP page and also the pseudocode from [http://colin.barker.pagesperso-orange.fr/lpa/big_squf.htm here].
<syntaxhighlight lang="raku" perl6line># 20210325 Raku programming solution
 
my @multiplier = ( 1, 3, 5, 7, 11, 3*5, 3*7, 3*11, 5*7, 5*11, 7*11, 3*5*7, 3*5*11, 3*7*11, 5*7*11, 3*5*7*11 );
Line 1,756:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,794:
 
squfof.raku
<syntaxhighlight lang="raku" perl6line># 20210326 Raku programming solution
 
use NativeCall;
Line 1,823:
) -> \data {
given squfof(data) { say data, " = ", $_, " * ", data div $_ }
}</langsyntaxhighlight>
{{out}}<pre>gcc -Wall -fPIC -shared -o LibSQUFOF.so squfof.c
file LibSQUFOF.so
Line 1,872:
within the
<br>REXX program) &nbsp; is because the number being tested is a prime &nbsp; (1,002,742,628,021).
<langsyntaxhighlight lang="rexx">/*REXX pgm factors an integer using Daniel Shanks' (1917-1996) square form factorization*/
numeric digits 100 /*ensure enough decimal digits.*/
call dMults 1,3,5,7,11,3*5,3*7,3*11,5*7,5*11,7*11, 3*5*7, 3*5*11, 3*7*11, 5*7*11, 3*5*7*11
Line 1,925:
if r\==1 then if r\==n then return r
end /*#*/
return 0</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default input:}}
<pre>
Line 1,969:
 
Even so, there are two examples which fail (1000000000000000127 and 1537228672809128917) because the code is unable to process enough 'multipliers' before an overflow situation is encountered. To deal with this, the program automatically switches to BigInt to process such cases.
<langsyntaxhighlight lang="ecmascript">import "/long" for ULong
import "/big" for BigInt
import "/fmt" for Fmt
Line 2,071:
var quot = (fact.isZero) ? "fail" : (N / fact).toString
Fmt.print("$-20s $-10s $s", N, fact, quot)
}</langsyntaxhighlight>
 
{{out}}
10,327

edits