Ad

We know that Finn is friends with Jake, Flame Princess is friends with Finn as is Princess Bubblegum, Jake is friends with Lady Unicorn and Princess Bubblegum.

Write a function friends(X) -> [Y] to find out who are the friends of a person X.

friend('Finn', 'Jake').
friend('Flame Princess', 'Finn').
friend('Princess Bubblegum', 'Finn').
friend('Jake', 'Lady Unicorn').
friend('Jake', 'Princess Bubblegum').

friends(X, R) :- findall(Y, friend(X, Y), R1), findall(Y, friend(Y, X), R2), append(R1, R2, R).
Code
Diff
  • hello :- write("Hello, Prolog!").
    • System.Console.WriteLine("Hello, C#!");
    • hello :- write("Hello, Prolog!").
Code
Diff
  • add(X, 0, X) :- !.
    add(0, Y, Y) :- !.
    add(X, 1, R) :- succ(X, R), !.
    add(1, Y, R) :- succ(Y, R), !.
    add(X, X, R) :- plus(X, X, R), !.
    add(X, Y, R) :- plus(X, Y, R).
    
    • def add(a, b):
    • return a + b
    • add(X, 0, X) :- !.
    • add(0, Y, Y) :- !.
    • add(X, 1, R) :- succ(X, R), !.
    • add(1, Y, R) :- succ(Y, R), !.
    • add(X, X, R) :- plus(X, X, R), !.
    • add(X, Y, R) :- plus(X, Y, R).
Code
Diff
  • is_odd(X) :- 1 is X mod 2.
    odd_count(N, R) :- is_odd(N) -> odd_count_recur(N, R); odd_count_recur(N + 1, R).
    odd_count_recur(0, 0) :- !.
    odd_count_recur(1, 0) :- !.
    odd_count_recur(N, R) :- N1 is N - 2, odd_count_recur(N1, R1), R is 1 + R1.
    
    • #import <Foundation/Foundation.h>
    • @interface SampleClass:NSObject
    • /* method declaration */
    • - (int)odd_count:(int)n;
    • @end
    • @implementation SampleClass
    • - (int) odd_count:(int) n{
    • //your code here
    • int odd_counter = 0;
    • for (int i = 1; i < n; i=i+2) {
    • // Numbers that are divisible by 2
    • if (i % 2 != 0)
    • odd_counter++;
    • }
    • return odd_counter;
    • }
    • @end
    • is_odd(X) :- 1 is X mod 2.
    • odd_count(N, R) :- is_odd(N) -> odd_count_recur(N, R); odd_count_recur(N + 1, R).
    • odd_count_recur(0, 0) :- !.
    • odd_count_recur(1, 0) :- !.
    • odd_count_recur(N, R) :- N1 is N - 2, odd_count_recur(N1, R1), R is 1 + R1.
Fundamentals
Numbers
Data Types
Integers
Code
Diff
  • is_odd(X, true) :- 1 is X /\ 1, !.
    is_odd(_X, false).
    • public static class Kata
    • {
    • public static bool IsOdd(int input)
    • {
    • return ((input % 10 == 1) || (input % 10 == 3) || (input % 10 == 5) || (input % 10 == 7) || (input % 10 == 9));
    • }
    • }
    • is_odd(X, true) :- 1 is X /\ 1, !.
    • is_odd(_X, false).
Code
Diff
  • div2(X, Y) :- Y is X >> 1.
    • unsigned long long div2(unsigned long long a){
    • //Use binary operators...I-O
    • return a >> 1;
    • }
    • div2(X, Y) :- Y is X >> 1.
Code
Diff
  • digit_sum = x => String(Math.abs(x)).split('').reduce((a, x) => a + +x, 0);
    • def digit_sum(number: int) -> int:
    • return(sum([int(num) for num in str(abs(number))]))
    • digit_sum = x => String(Math.abs(x)).split('').reduce((a, x) => a + +x, 0);