You are on page 1of 1

%Part A.

isFactorOf(Factor,Factor).
isFactorOf(Factor,Bignumber) :-
\+Factor=0,
Bignumber mod Factor =:= 0.
:- op(900,xfy,isFactorOf).

%Part B.
isFactorCascade(Factor, Bignumber) :- %Case where Factor is a factor.
isFactorOf(Factor,Bignumber). %Simple call to check if factor. Returns true
if so.
isFactorCascade(Factor, Bignumber) :- %Case where Factor is not a factor.
Factor>2, %Stop if Factor approaches 'useless' numbers.
isFactorCascade(Factor-1, Bignumber). %Recursion.

isPrime(X) :- X=2.
isPrime(X) :- \+ isFactorCascade(X-1,X). %Needs a separate function to allow for
having only one variable input.

%Part C. Infinite loops for some reason.


gcd(Int1,Int2,Divisor) :- Int1=Int2, Divisor=Int1.
gcd(Int1,Int2,Divisor) :- Int2=0, Divisor=Int1.
gcd(Int1,Int2,Divisor) :- Int3 = Int1 mod Int2, gcd(Int2,Int3,Divisor).

%Part D.
%First, make an insertion predicate.
insert(X,[],[X]).
insert(X,[H|T],[X,H|T]) :- X<H.
insert(X,[H|T1],[H|T2]) :- insert(X,T1,T2).

%Then abuse the heck out of it.


insersort([],[]).
insersort([H|T],Final) :- insersort(T,Progress), insert(H,Progress,Final).

%Part E. It's badly made, and doesn't return what it should.


listsplit(L,List1,List2) :- append(List1,List2,L), length(List1,Length),
length(List2,Length).
listsplit(L,List1,List2) :- append(List1,List2,L), length(List1,Length),
Length2=Length+1, length(List2,Length2).
mergesort([],[]).
mergesort([X],[X]).
mergesort(X,Final) :- listsplit(X,List1,List2), mergesort(List1,Part1),
mergesort(List2,Part2), insert(Part1,Part2,Final).

You might also like