Largest prime factor: Difference between revisions

Added Easylang
No edit summary
(Added Easylang)
 
(11 intermediate revisions by 9 users not shown)
Line 5:
<br>What is the largest prime factor of the number 600851475143 ?
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
F isPrime(n)
L(i) 2 .. Int(n ^ 0.5)
I n % i == 0
R 0B
R 1B
 
V n = 600851475143
V j = 3
L !isPrime(n)
I n % j == 0
n I/= j
j += 2
print(n)
</syntaxhighlight>
 
{{out}}
<pre>
6857
</pre>
 
=={{header|ALGOL 68}}==
Line 47 ⟶ 71:
Largest prime factor of 751 is 751
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">print max factors.prime 600851475143</syntaxhighlight>
 
{{out}}
 
<pre>6857</pre>
 
=={{header|AutoHotkey}}==
Line 113 ⟶ 145:
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
Código sacado de https://www.calormen.com/jsbasic/ El código original es de Christiano Trabuio.
<syntaxhighlight lang="qbasic">100 HOME
110 LET a = 600851475143
120 LET f = 0
130 IF a/2 = INT(a/2) THEN LET a = a/2: LET f = 1: GOTO 130
140 LET i = 3
150 LET e = INT(SQR(a)) + 2
160 LET f = 0
170 FOR n = i TO e STEP 2
180 IF a /n = INT(a/n) THEN LET a = a / n: LET i = n: LET n = e: LET f = 1
190 NEXT n
200 IF a > n AND f <> 0 THEN GOTO 160
210 PRINT a
220 END</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|BASIC256}}===
<syntaxhighlight lang="basic">#No primality testing is even required.
n = 600851475143
j = 3
do
if int(n/j) = n/j then n /= j
j += 2
until j = n
print n</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|Applesoft BASIC}}
<syntaxhighlight lang="basic">100 CLS : REM 10 HOME para Applesoft BASIC
110 LET a = 600851475143
120 LET f = 0
130 IF a/2 = INT(a/2) THEN LET a = a/2: LET f = 1: GOTO 130
140 LET i = 3
150 LET e = INT(SQR(a)) + 2
160 LET f = 0
170 FOR n = i TO e STEP 2
180 IF a /n = INT(a/n) THEN LET a = a / n: LET i = n: LET n = e: LET f = 1
190 NEXT n
200 IF a > n AND f <> 0 THEN GOTO 160
210 PRINT a
220 END</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#include"isprime.bas"
Line 133 ⟶ 214:
100 PRINT N#</syntaxhighlight>
{{output}}<pre>6857</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">REM No primality testing is even required.
DIM a AS DOUBLE
a = 600851475143#
i = 3
DO
IF INT(a / i) = a / i THEN a = a / i
i = i + 2
LOOP UNTIL a = i ' o WHILE a <> i
PRINT a</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="vb">function isPrime(n)
if n < 2 then isPrime = 0 : goto [exit]
if n = 2 then isPrime = 1 : goto [exit]
if n mod 2 = 0 then isPrime = 0 : goto [exit]
isPrime = 1
for i = 3 to int(n^.5) step 2
if n mod i = 0 then isPrime = 0 : goto [exit]
next i
[exit]
end function
 
n = 600851475143
j = 3
while isPrime(n) <> 1
if n mod j = 0 then n = n / j
j = j +2
wend
print n
 
'But, no primality testing is even required.
n = 600851475143
j = 3
while j <> n
if int(n/j) = n / j then n = n / j
j = j +2
wend
print n
end</syntaxhighlight>
{{out}}
<pre>6857
6857</pre>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">!No primality testing is even required.
LET n = 600851475143
LET j = 3
DO WHILE j <> n
IF INT(n/j) = n / j THEN LET n = n / j
LET j = j + 2
LOOP
PRINT n
END</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "LPF"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
'No primality testing is even required.
DOUBLE n
n = 600851475143
j = 3
 
DO
IF INT(n/j) = n/j THEN n = n / j
j = j + 2
LOOP UNTIL j = n
PRINT n
END FUNCTION
END PROGRAM</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="basic">//No primality testing is even required.
n = 600851475143
j = 3
repeat
if int(n/j) = n/j n = n / j
j = j + 2
until j = n
print n</syntaxhighlight>
{{out}}
<pre>6857</pre>
 
=={{header|BCPL}}==
Line 246 ⟶ 423:
The largest prime factor of 600,851,475,143 is 6857
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Math,SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
function IsPrime(N: int64): boolean;
{Fast, optimised prime test}
var I,Stop: int64;
begin
if (N = 2) or (N=3) then Result:=true
else if (n <= 1) or ((n mod 2) = 0) or ((n mod 3) = 0) then Result:= false
else
begin
I:=5;
Stop:=Trunc(sqrt(N+0.0));
Result:=False;
while I<=Stop do
begin
if ((N mod I) = 0) or ((N mod (I + 2)) = 0) then exit;
Inc(I,6);
end;
Result:=True;
end;
end;
 
 
function GetLargestPrimeFact(N: int64): int64;
var J: int64;
begin
J:=3;
while not IsPrime(N) do
begin
if (N mod j) = 0 then N:=N div J;
Inc(J,2);
end;
Result:=N;
end;
 
 
procedure TestLargePrimeFact(Memo: TMemo);
var F: integer;
begin
F:=GetLargestPrimeFact(600851475143);
Memo.Lines.Add(IntToStr(F));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
6857
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
n = 600851475143
i = 2
while n > i
if n mod i = 0
n = n div i
.
i += 1
.
print n
</syntaxhighlight>
{{out}}
<pre>
6857
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">
Line 272 ⟶ 522:
The largest prime factor of 600,851,475,143 is 6857
</pre>
 
=={{header|Erlang}}==
Uses a factorization wheel, but without builtin lazy lists, it's rather awkward for a functional language.
Line 513 ⟶ 764:
print(n);</syntaxhighlight>
 
=={{header|Quackery}}==
 
<code>primefactors</code> is defined at [[Prime decomposition#Quackery]].
 
<syntaxhighlight lang="Quackery">600851475143 primefactors -1 peek echo</syntaxhighlight>
 
{{out}}
 
<pre>6857</pre>
 
=={{header|R}}==
Line 519 ⟶ 779:
sieve <- function(n) {
if (n < 2)
return (NULL)
primes <- rep(TRUE, n)
Line 525 ⟶ 785:
 
for (i in 1:floor(sqrt(n)))
if (primes[i])
primes[seq(i*i, n, by = i)] <- FALSE
which(primes)
Line 535 ⟶ 795:
factors <- primes[n %% primes == 0]
if (length(factors) == 0)
n
n
else {
for (p in factors) { # add all elements of n/p that are also prime
d <- n / p
if (d != p && all(d %% primes[primes <= floor(sqrt(d))] != 0))
factors <- c(factors, d)
}
}
factors
}
}
 
cat("The prime factors of 600,851,475,143 are", paste(prime.factors(600851475143), collapse = ", "), "\n")
 
</syntaxhighlight>
{{Out}}
Line 706 ⟶ 967:
6857
done...
</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
The task can be solved directly by a command line:
600851475143 FACTORS 1 GET
{{out}}
<pre>1: 6857
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
p 600851475143.prime_division.last.first</syntaxhighlight>
{{out}}
<pre>6857
</pre>
 
Line 735 ⟶ 1,012:
=={{header|Wren}}==
Without using any library functions at all (it's a one-liner otherwise):
<syntaxhighlight lang="ecmascriptwren">var largestPrimeFactor = Fn.new { |n|
if (!n.isInteger || n < 2) return 1
var inc = [4, 2, 4, 2, 4, 6, 2, 6]
1,983

edits