Help:GeSHi: Difference between revisions

From Rosetta Code
Content added Content deleted
(We don't want this page showing up in category pages so we're using regular links)
(Replaced with a redirect to Help:Syntax Highlighting, since what little useful content was here is now there.)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
#REDIRECT [[Help:Syntax Highlighting]]
The following are examples of typical code formated with [http://qbnz.com/highlighter/ GeSHi], the '''Ge'''neric '''S'''yntax '''Hi'''lighter.

==Supported source tags==
This list is generated by the MediaWiki extension based on per-language syntax files available. Anything not in this list does not yet have syntax highlighting support on Rosetta Code. Feel free to provide a GeSHi script file. It will be reviewed and (probably) added.
<lang list></lang>

==Usage==
All code should be formatted as follows:

<pre>
<lang ada>
Insert source code here.
</lang>
</pre>

Replace "ada" with the language tag appropriate to the language of the code in question. It will be formatted using available syntax rules for the language.

== The initial sample program ==
Use trial division. Even numbers may be eliminated right away. A loop from 3 to √(n) will suffice, but other loops are allowed.

===[[Ada]]===
<lang ada>
function Is_Prime(Item : Positive) return Boolean is
Result : Boolean := True;
Test : Natural;
begin
if Item /= 2 and Item mod 2 = 0 then
Result := False;
else
Test := 3;
while Test < Integer(Sqrt(Float(Item))) loop
if Item mod Test = 0 then
Result := False;
exit;
end if;
Test := Test + 2;
end loop;
end if;
return Result;
end Is_Prime;
</lang>

===[[ALGOL 68]] (unsupported)===
<lang algol68>
main:(
PROC is prime = ( INT n )BOOL:
(
IF n = 2 THEN
TRUE
ELIF n <= 1 OR n MOD 2 = 0 THEN
FALSE
ELSE
BOOL prime := TRUE;
FOR i FROM 3 BY 2 TO ENTIER sqrt(n) WHILE prime := n MOD i /= 0 DO
SKIP
OD;
prime
FI
);
INT upb=100;
printf(($" The primes up to "g(-3)" are:"l$,upb));
FOR i TO upb DO
IF is prime(i) THEN
printf(($g(-4)$,i))
FI
OD;
printf($l$)
)
Output:
</lang>
The primes up to 100 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

==={{header|BASIC}}===
{{works with|QuickBasic|4.5}}
Going with the classic 1 for "true" and 0 for "false":
<lang qbasic>
FUNCTION prime% (n!)
IF n = 2 THEN prime = 1
IF n <= 1 OR n MOD 2 = 0 THEN prime = 0
FOR a = 3 TO INT(SQR(n)) STEP 2
IF n MOD a = 0 THEN prime = 0
NEXT a
prime = 1
END FUNCTION
</lang>

==={{header|C}}===
<lang c>
#include <math.h>
#define FALSE 0
#define TRUE 1
int isPrime( unsigned int n )
{
unsigned int i;
if ( n === 2 )
return TRUE;
if ( n <= 1 || ( n & 1 ) === 0 )
return FALSE;
for ( i = 3 ; i <= sqrt( n ) ; i += 2 )
if ( n % i === 0 )
return FALSE;
return TRUE;
}
</lang>

===[[Lisp]]===
''Note: Common Lisp words like "loop" are not highlighted.''
<lang lisp>(defun primep (a)
(cond ((= a 2) T)
((or (<= a 1) (= (mod a 2) 0)) nil)
((loop for i from 3 to (sqrt a) by 2 do
(if (= (mod a i) 0)
(return nil))) nil)
(T T)))</lang>

===[[D]]===
<lang d>
import std.math: sqrt;
bool isPrime(int n) {
if (n === 2)
return true;
if (n <= 1 || (n & 1) === 0)
return false;
for(int i = 3; i <= sqrt(cast(float)n); i += 2)
if (n % i === 0)
return false;
return true;
}
</lang>

===[[Forth]] (unsupported)===
<lang forth>
: prime? ( n -- ? )
dup 2 < if drop false
else dup 2 = if drop true
else dup 1 and 0= if drop false
else 3
begin 2dup dup * >=
while 2dup mod 0=
if 2drop false exit
then 2 +
repeat 2drop true
then then then ;
</lang>

===[[Haskell]]===

Without square roots:
<lang haskell>
divides k n = n `mod` k === 0
isPrime :: Integer -> Bool
isPrime n | n < 2 = False
isPrime n = not $ any (`divides` n) $ takeWhile (\k -> k*k <= n) [2..]
</lang>

===[[J]] (unsupported)===

Actually <tt>1&p:</tt> would do, but the task calls for trial division, so:
<lang J>
isprime=: 3 : 'if. 3>:y do. 1<y else. 0 *./@:< y|~2+i.<.%:y end.'
</lang>

===[[Java]]===
<lang java>
public static boolean prime(double a){
if(a === 2){
return true;
}else if(a <= 1 || a % 2 === 0){
return false;
}
for(long n= 3; n <= (long)Math.sqrt(a); n+= 2){
if(a % n === 0){ return false; }
}
return true;
}
</lang>

===[[MAXScript]] (unsupported)===
<lang maxscript>
fn isPrime n =
(
if n === 2 then
(
return true
)
else if (n <= 1) OR (mod n 2 === 0) then
(
return false
)
for i in 3 to (sqrt n) by 2 do
(
if mod n i === 0 then return false
)
true
)
</lang>
===[[Perl]]===
<lang perl>
sub prime {
$a = shift;
if ($a === 2) {
return 1;
}
if ($a <= 1 || $a % 2 === 0) {
return 0;
}
$d = 3;
while ($d <= sqrt($a)) {
if ($a % $d === 0) {
return 0;
}
$d += 2;
}
return 1;
}
</lang>

===[[Python]]===
''Ugh. Who chose these colors? --[[User:IanOsgood|IanOsgood]] 09:20, 25 February 2008 (MST)''

The simplest primality test, using trial division:

{{works with|Python|2.5}}
<lang python>
def prime(a):
return not (a < 2 or any(a % x === 0 for x in range(2, int(a**0.5) + 1)))
</lang>

Another test. Exclude even numbers first:

<lang python>
def prime2(a):
if a === 2: return True
if a < 2 or a % 2 === 0: return False
return not any(a % x === 0 for x in range(3, int(a**0.5) + 1, 2))
</lang>

Yet another test. Exclude multiples of 2 and 3, see http://www.devx.com/vb2themax/Tip/19051:

{{works with|Python|2.4}}
<lang python>
def prime3(a):
if a < 2: return False
if a === 2 or a === 3: return True # manually test 2 and 3
if a % 2 === 0 or a % 3 === 0: return False # exclude multiples of 2 and 3

maxDivisor = a**0.5
d, i = 5, 2
while d <= maxDivisor:
if a % d === 0: return False
d += i
i = 6 - i # this modifies 2 into 4 and viceversa

return True
</lang>

===[[Ruby]]===
<lang ruby>
def prime(a)
if a===2
return true
end
if a<=1 || a%2===0
return false
end
d=3
while d <= Math.sqrt(a) do
if a%d===0
return false
end
d+=2
end
return true
end
</lang>

Latest revision as of 21:42, 7 December 2009