2
0
Fork 0
mirror of https://github.com/MartinThoma/LaTeX-examples.git synced 2025-04-25 06:18:05 +02:00

added another prolog example

This commit is contained in:
Martin Thoma 2014-09-16 16:56:23 +02:00
parent 87fe3eb2b5
commit 6829f6cf97
5 changed files with 90 additions and 0 deletions

View file

@ -34,6 +34,14 @@ und $Y$ Farben sind und $X \neq Y$ gilt:
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/simple-term.pl} \inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/simple-term.pl}
\subsection{Kommentare}\xindex{Kommentare (Prolog)}
Prolog kennt zwei Kommentar-Typen:
\begin{itemize}
\item Zeilen-Kommentare, die mit \verb+%+ beginnen
\item Block-Kommentare, diem durch \verb+/* ... */+ markiert werden.
\end{itemize}
\subsection{= und ==}\xindex{= (Prolog)}\xindex{== (Prolog)} \subsection{= und ==}\xindex{= (Prolog)}\xindex{== (Prolog)}
In Prolog entspricht \texttt{=} dem Prädikat \texttt{=/2}. Das Prädikat \texttt{<a> = <b>} wird In Prolog entspricht \texttt{=} dem Prädikat \texttt{=/2}. Das Prädikat \texttt{<a> = <b>} wird
erfüllt, wenn die beiden Terme \texttt{<a>} und \texttt{<b>} unifiziert werden erfüllt, wenn die beiden Terme \texttt{<a>} und \texttt{<b>} unifiziert werden
@ -211,6 +219,10 @@ bei Prof. Dr. Snelting:
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/regex.pl} \inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/regex.pl}
\subsection{Two Bases}
\inputminted[numbersep=5pt, tabsize=4]{prolog}{scripts/prolog/01-two-bases.prolog}
\section{Weitere Informationen} \section{Weitere Informationen}
\begin{itemize} \begin{itemize}
\item \href{http://wiki.ubuntuusers.de/Prolog}{\path{wiki.ubuntuusers.de/Prolog}}: Hinweise zur Installation von Prolog unter Ubuntu \item \href{http://wiki.ubuntuusers.de/Prolog}{\path{wiki.ubuntuusers.de/Prolog}}: Hinweise zur Installation von Prolog unter Ubuntu

View file

@ -154,6 +154,12 @@ $a \cdot b$ zu erhalten?
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=main.scala]{scala}{scripts/scala/HighProduct.scala} \inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=main.scala]{scala}{scripts/scala/HighProduct.scala}
\subsection{Potenzierung}
Will man Zweierpotenzen bilden, so kann man die Berechnung beschleunigen, in dem
man immer wieder Quadriert:
\inputminted[linenos, numbersep=5pt, tabsize=4, frame=lines, label=power-futures.scala]{scala}{scripts/scala/power-futures.scala}
\section{Weitere Informationen} \section{Weitere Informationen}
\begin{itemize} \begin{itemize}
\item \url{http://www.scala-lang.org/api} \item \url{http://www.scala-lang.org/api}

View file

@ -0,0 +1,15 @@
#!/usr/bin/swipl -q -t main -f
% Find three digits X, Y and Z such that
% XYZ in base10 is equal to ZYX in base9
is_solution(X, Y, Z) :- between(0,9,X),
between(0,9,Y),
between(0,9,Z),
Base10 is (100*X + 10*Y + Z),
Base9 is (9*9*Z+9*Y+X),
Base10 = Base9.
main :-
is_solution(X, Y, Z),
format("solution: ~w ~w ~w\n", [X,Y,Z]),
false. % make sure that all solutions get printed

View file

@ -0,0 +1,57 @@
import scala.actors.Futures._;
object FastPower {
/**
* Calculate a power of two fast.
*/
def fastPow(x: Int, n: Int): Long = {
var result = 1L;
val b = n.toBinaryString.reverse;
for(d <- 0 until b.length()) {
if(b.charAt(d).equals('1')){
result *= scala.math.pow(x,
scala.math.pow(2, d)).toLong;
}
}
return result;
}
/**
* Calculate a power of two fast and use Futures.
*/
def fastPowParallel(x: Int, n: Int): Long = {
var result = 1L;
val b = n.toBinaryString.reverse;
val tasks = for (d <- 0 until b.length())
yield future
{
var interim = 0L;
if (b.charAt(d).equals('1')){
interim = scala.math.pow(x,
scala.math.pow(2, d)).toLong;
}
interim;
}
val futureRes = awaitAll(20000L, tasks: _*);
futureRes.foreach { res =>
res match {
case Some(x: Long) => if (x > 0)
result *= x
case None => throw new
Exception("error")
};
}
return result;
}
def main(args: Array[String]) {
println(fastPowParallel(2, 9));
// => 512
}
}