r/dailyprogrammer • 2 0 • Mar 02 '18

Weekly #28 - Mini Challenges

So this week, let's do some mini challenges. Too small for an easy but great for a mini challenge. Here is your chance to post some good warm up mini challenges. How it works. Start a new main thread in here.

if you post a challenge, here's a template we've used before from /u/lengau for anyone wanting to post challenges (you can copy/paste this text rather than having to get the source):

**[CHALLENGE NAME]** - [CHALLENGE DESCRIPTION]

**Given:** [INPUT DESCRIPTION]

**Output:** [EXPECTED OUTPUT DESCRIPTION]

**Special:** [ANY POSSIBLE SPECIAL INSTRUCTIONS]

**Challenge input:** [SAMPLE INPUT]

If you want to solve a mini challenge you reply in that thread. Simple. Keep checking back all week as people will keep posting challenges and solve the ones you want.

Please check other mini challenges before posting one to avoid duplications (within reason).

95 Upvotes

55 comments sorted by

View all comments

10

u/[deleted] Mar 02 '18

[nth number of Recamán's Sequence] - Recamán's Sequence is defined as "a(0) = 0; for n > 0, a(n) = a(n-1) - n if positive and not already in the sequence, otherwise a(n) = a(n-1) + n." (Note: See this article for clarification if you are confused).

Given: a positive integer n.

Output: the nth digit of the sequence.

Special: To make this problem more interesting, either golf your code, or use an esoteric programming language (or double bonus points for doing both).

Challenge input: [5, 15, 25, 100, 1005]

27

u/[deleted] Mar 03 '18 edited Mar 04 '18

dogescript

shh THIS IS DOGESCRIPT

such recaman much nth
    very seen is new Array with 0
    seen dose push with 0
    much very n as 1 next n smaller nth+1 next n more 1
        very spot is seen[n - 1]
        very lower is spot-n
        rly lower bigger 0 and !seen.includes(lower)
            seen dose push with lower
        but
            seen dose push with spot+n
        wow
    wow
    very place is seen[nth]
    plz console.loge with place
wow place

3

u/wertperch Mar 20 '18

DOGESCRIPT

Do I have to say such as "much wow" now?

2

u/Skyhawk_Squawk Mar 24 '18

Should be called DOGECODE

→ More replies (1)

3

u/M4D5-Music Mar 02 '18 edited Mar 02 '18

Java 8
A simple solution (solveSmall), and a memory optimized solution (solveBig) that uses ranges rather than a set. solveBig is a few orders of magnitude slower, but at some point it should get faster than solveSmall if the number is big enough. solveSmall would probably use all the memory on the machine first though.

edit: found out that NavigableMap::floorEntry (TreeMap implementation) has pretty fast access time; using it for solveBig makes it almost half as fast as solveSmall at lower numbers, and it seems to pass solveSmall at about 10 million.

Both have the following output:

a(5) = 7
a(15) = 24
a(25) = 17
a(100) = 164
a(1005) = 2683

Code

import com.google.common.collect.Range;

import java.util.*;

public class Recaman {

    public static void main(String[] args) {
        long target;
        try (Scanner s = new Scanner(System.in)) {
            target = s.nextLong();
        }

        System.out.println(solveBig(target));
    }

    private static long solveSmall(long target) {
        long current = 0;
        Set<Long> encountered = new HashSet<>();
        for (long i = 1; i < target + 1; i++) {
            encountered.add(current);
            long minusAttempt = current - i;
            if (minusAttempt >= 0 && !encountered.contains(minusAttempt)) {
                current = minusAttempt;
            } else {
                current = current + i;
            }
        }
        return current;
    }

    private static long solveBig(long target) {
        long current = 0;
        TreeMap<Long, Range<Long>> encountered = new TreeMap<>();
        encountered.put(0L, Range.singleton(0L));
        long optimizeInterval = 1000;

        for (long i = 1; i < target + 1; i++) {

            long minusAttempt = current - i;
            if (minusAttempt >= 0 && notContainedInRanges(minusAttempt, encountered)) {
                current = minusAttempt;
                encountered.put(current, Range.singleton(current));
            } else {
                current = current + i;
                if (notContainedInRanges(current, encountered)) {
                    encountered.put(current, Range.singleton(current));
                }
            }

            // Do the first optimization at 100
            if (i % optimizeInterval == 100) {
                encountered = optimizeRanges(encountered);
                optimizeInterval *= 1.01; // 1.01 is arbitrary, maybe faster with some tuning.
            }
        }

        return current;
    }

    private static boolean notContainedInRanges(Long target, NavigableMap<Long, Range<Long>> ranges) {
        Map.Entry<Long, Range<Long>> range = ranges.floorEntry(target);
        return range == null || target > range.getValue().upperEndpoint();
    }

    // Combine consecutive numbers into the same range
    private static TreeMap<Long, Range<Long>> optimizeRanges(TreeMap<Long, Range<Long>> rangesOrig) {
        if(rangesOrig.size() == 0) {
            return rangesOrig;
        }

        TreeMap<Long, Range<Long>> ranges = new TreeMap<>();
        Range<Long> currentRange = rangesOrig.get(0L);
        rangesOrig.remove(0L);

        while (rangesOrig.values().size() > 0) {
            if (rangesOrig.containsKey(currentRange.upperEndpoint() + 1)) {
                Range<Long> range =  rangesOrig.get(currentRange.upperEndpoint() + 1);
                currentRange = Range.closed(currentRange.lowerEndpoint(), range.upperEndpoint());
                rangesOrig.remove(range.lowerEndpoint());
            } else {
                ranges.put(currentRange.lowerEndpoint(), currentRange);
                currentRange = rangesOrig.values().iterator().next();
                rangesOrig.remove(currentRange.lowerEndpoint());
            }
        }

        ranges.put(currentRange.lowerEndpoint(), currentRange);

        return ranges;
    }
}
→ More replies (2)

3

u/[deleted] Mar 02 '18 edited Mar 02 '18

Haskell

recaman :: Int -> [Int]
recaman 0 = [0]
recaman n =
  let n' = recaman (n - 1)
      nl = last n'
  in
    if nl - n >= 0 && (not $ any (==nl-n) n') then
      n' ++ [nl - n]
    else
      n' ++ [nl + n]

nth_recaman :: Int -> Int
nth_recaman = last . recaman       

2

u/macgillebride Mar 04 '18

Haskell as well, but defining the list recursively

import Data.List (inits)
import Data.Foldable (for_)

recamans = 0 : [let x  = recamans !! (i-1) - i
                    x' = recamans !! (i-1) + i
                in if x > 0 &&
                      not (x `elem` (inits recamans) !! i)
                   then x
                   else x' | i <- [1..]]

main :: IO ()
main = do
  for_ [5, 15, 25, 100, 1005] $ \i ->
    putStrLn . show $ recamans !! i

3

u/Kendrian Mar 07 '18 edited Mar 07 '18

Golfed in Julia:

rseq(n) = n == 0 ? ([0], Set(0)) : begin
    (seq, vals) = rseq(n-1)
    x = seq[end] - n
    x <= 0 || x in vals ? x += 2*n : nothing
    (push!(seq, x), push!(vals, x))
end
    
recaman(n) = rseq(n)[1][end]

map(recaman, [5, 15, 25, 100, 1005])

Or, the slightly less golfed but now resistant to stack overflow version:

rseq(n) = n == 0 ? [0] : begin
    (s, v) = ([0], Set(0))
    for i = 1:n
        x = s[end] - i
        x <= 0 || x in v ? x += 2*i : nothing
        (push!(s, x), push!(v, x))
    end
    s
end

This computes the sequence to 10_000_000 terms in about 2 and a half seconds.

2

u/ASpueW Mar 02 '18

Rust playground

use std::collections::BTreeSet;

#[derive(Default)]
struct Recaman{
    idx: usize, 
    val: usize, 
    seq: BTreeSet<usize>
}

impl Iterator for Recaman {
    type Item = usize;
    fn next(&mut self) -> Option<usize>{
        let Recaman{ idx, val, ref mut seq } = *self; 
        self.val = if val > idx && !seq.contains(&(val - idx)) { val - idx } else { val + idx };
        self.idx += 1;
        seq.insert(self.val); 
        Some(self.val)       
    }
}

fn main() {
    println!("first 10 members:");
    for (i, n) in Recaman::default().enumerate().take(10) {
        println!("a({}) = {}", i, n);
    }

    println!("\nchallenge output:");
    for &n in &[5, 15, 25, 100, 1005] {
        println!("a({}) = {}", n, Recaman::default().nth(n).unwrap());
    }
}

Challenge output:

a(5) = 7
a(15) = 24
a(25) = 17
a(100) = 164
a(1005) = 2683

2

u/nthai Mar 02 '18 edited Mar 02 '18

Mathematica

So I just used some memoization to always remember the nth value and as usual, tried to avoid loops in Mathematica. Still not a really good solution because it recreates a list on each new call. Also had to increase the recursion limit for larger numbers.

Edit: there is no need to increase the recursion limit, I'm just dumb.

Recamán[0] = 0;
Recamán[n_] := Recamán[n] = (p = Recamán[n - 1] - n; If[p > 0 && ! MemberQ[Recamán /@ Range[0, n - 1], p], p, p + 2 n]);
Block[{$RecursionLimit = 5000}, Recamán /@ {5, 15, 25, 100, 1005}]

Output

{7, 24, 17, 164, 2683}

2

u/engageant Mar 02 '18 edited Mar 02 '18

Posh

Golf solution (156)

 $p=0;$i=1;$r=[ordered]@{};$r.Add(0,0);1..1006|%{$w=$p-$i;$s=$p+$i;if($w-ge0-and$w-notin$r.Values){$r.Add($i,$w);$p=$w;$i++}else{$r.Add($i,$s);$p=$s;$i++}}

Clean solution:

$previous = 0
$i = 1
$sequence = [ordered]@{}
$sequence.Add(0,0)

1..1006 | % {
    $want = $previous - $i
    $settle = $previous + $i    
    if ($want -ge 0 -and $want -notin $sequence.Values) {
        $sequence.Add($i, $want)
        $previous = $want
        $i++
    }
    else {
        $sequence.Add($i, $settle)
        $previous = $settle
        $i++
    }       
}

2

u/vault_dweller_707 Mar 02 '18

Python 3.6
Feedback very welcome.

Code:

def seq(n):  
    s = [0]  
    rseq = lambda i: s.append(s[-1]-i) if s[-1]-i>0 and s[-1]-i not in s else s.append(s[-1]+i)  
    [rseq(j) for j in range(1,n+1)]  
    return s[-1]  

Output:

7  
24  
17  
164  
2683  
→ More replies (1)

2

u/zqvt Mar 02 '18 edited Mar 02 '18

Clojure

(defn next-step [xs x]
  (let [diff (- (last xs) x)]
    (if (and (> diff 0)
             (not (.contains xs diff)))
      diff
      (+ (last xs) x))))

(defn recaman-seq [goal]
  (loop [x 1 xs [0]]
    (if (> x goal)
      (last xs)
      (recur (inc x) (conj xs (next-step xs x)))))

2

u/gabyjunior 1 2 Mar 02 '18

C Using an AVL tree to store the values already used in the sequence.

#include <stdio.h>
#include <stdlib.h>
#include "avl_tree.h"

int compare_values(void *, void *);

int main(void) {
int order, *values, i;
node_t *nodes;
    if (scanf("%d", &order) != 1 || order < 0) {
        fprintf(stderr, "Invalid order\n");
        fflush(stderr);
        return EXIT_FAILURE;
    }
    values = malloc(sizeof(int)*(size_t)(order+1));
    if (!values) {
        fprintf(stderr, "Could not allocate memory for values\n");
        fflush(stderr);
        return EXIT_FAILURE;
    }
    compare_keys = compare_values;
    values[0] = 0;
    nodes = insert_node(NULL, values);
    if (!nodes) {
        free(values);
        return EXIT_FAILURE;
    }
    for (i = 1; i <= order; i++) {
        node_t *nodes_tmp;
        values[i] = values[i-1]-i;
        if (values[i] < 0 || get_node(nodes, values+i)) {
            values[i] = values[i-1]+i;
        }
        nodes_tmp = insert_node(nodes, values+i);
        if (!nodes_tmp) {
            free_node(nodes);
            free(values);
            return EXIT_FAILURE;
        }
        nodes = nodes_tmp;
    }
    if (i == order+1) {
        printf("%d\n", values[order]);
        fflush(stdout);
    }
    free_node(nodes);
    free(values);
    return EXIT_SUCCESS;
}

int compare_values(void *a, void *b) {
    int *value_a = (int *)a, *value_b = (int *)b;
    return *value_a-*value_b;
}

Challenge output

7
24
17
164
2683

Takes 0.5 secs for n = 1_000_000, 5.5 secs for n = 10_000_000.

The AVL tree library source code

2

u/[deleted] Mar 03 '18

Python 3.6

def reca(n):
    r = 0
    elements = {0}
    for i in range(1, n+1):
        r -= i
        if r < 0 or r in elements:
            r += 2*i
        elements.add(r)
    return r  

Challenge input results:

7
24
17
164
2683

2

u/chunes 1 2 Mar 04 '18

Factor

USING: kernel math prettyprint sequences ;
IN: dailyprogrammer.recaman

: a(n-1)|n ( seq -- a(n-1) n ) [ last ] [ length ] bi ;
: a(n-1)-n ( seq -- n )        a(n-1)|n - ;
: a(n-1)+n ( seq -- n )        a(n-1)|n + ;

: next-recaman ( seq -- n ) dup dup a(n-1)-n dup 0 >=
    [ swap member? not ] dip and [ a(n-1)-n ] [ a(n-1)+n ] if ;
    
: recaman ( n -- seq )
    V{ 0 } swap [ dup next-recaman over push ] times ;
    
: main ( -- ) { 5 15 25 100 1005 } dup last recaman swap
    [ swap nth . ] with each ;

MAIN: main

Output:

7
24
17
164
2683

2

u/pheipl Mar 07 '18 edited Mar 07 '18

Java 8

I loved this exercise! First of all, I had a blast deciding between recurrence and building from 0. Since this is a non deterministic equation, building it is absolutely necessary. In reality I wouldn't build it every time, just to a huge number, save it somewhere, and just lookup a(n), if it doesn't exist, extend it to n and save.

I also refused to use an array or a list, so I had to find something more convenient (and fast). TreeSet is perfect, never used it before, but it suits the needs of this exercise like a glove. First of all it's ordered, and I need to find the lowest occurrence and exit. It also doesn't save duplicates, which I don't need in any way. As close to perfect as a pre built, general purpose list could have possibly been.

I feel like the loop in buildRecaman is quite efficient, I'd love to be corrected as long as we don't get into type optimization or crazy things, I'm only talking about a logic level.

I had fun 😊

public class Main {

    public static void main(String[] args) {
        Long target = 0L;

        try (Scanner s = new Scanner(System.in)) {

            while (target >= 0L) {

                System.out.print("> ");
                target = s.nextLong();
                System.out.println(target + " -> " + Main.buildRecaman(target));

            }
        }

    }

    private static Long buildRecaman(Long target) {

        Set<Long> appearance = new TreeSet<>();
        Long previous = 0L;
        Long n = 1L;
        Long recaman = 0L;

        appearance.add(0L);

        while (n <= target) {

            recaman = previous - n;
            if (recaman < 0 || appearance.contains(recaman))
                recaman = previous + n;
            appearance.add(recaman);
            previous = recaman;
            n++;

        }

        return recaman;

    }
}

1

u/octolanceae Mar 02 '18

** C++17 **

Yeah, I know, not very esoteric, but, I don't have time to pull a new language out of the hat, learn it and throw down.

#include <iostream>
#include <vector>
#include <algorithm>


size_t gen_rec(size_t n, std::vector<size_t>* r) {
  if (r->back() >=  n) {
    auto it = std::find(r->begin(), r->end(), (r->back() - n));
    return ((it != r->end()) ? (r->back() + n) : (r->back() - n));
  } else
    return (r->back() + n);
}

size_t gen_rec_start(size_t n, std::vector<size_t>* rec) {
  if (n <= rec->size())
    return rec->at(n);
  for (size_t i = rec->size(); i <= n; i++)
    rec->push_back(gen_rec(i, rec));
  return rec->back();
}

int main() {
  const std::vector<size_t> tests{5, 15, 25, 100, 1005};
  std::vector<size_t> rec_nums{0};
  for (auto t: tests)
    std::cout << gen_rec_start(t, &rec_nums) << '\n';
}

** Output **

7
24
17
164
2683

1

u/Scara95 Mar 04 '18 edited Mar 04 '18

Q 67 65 61 60

Using indexing instead of first saves 2 characters, not that much of a change

Using (,) instead of enlist saves 4 characters

Using (#:) instead of count saves 1 charaters

{{i:(#:)x;n:x[0]-i;?[(n<0)|n in x;x[0]+i;n],x}/[x;(,)0][0]}

67 characters, no golfing done besides choosing short names for variables and using implicit argument names (x) for function arguments

{first{i:count x;n:x[0]-i;?[(n<0)|n in x;x[0]+i;n],x}/[x;enlist 0]}

example:

q){first{i:count x;n:x[0]-i;?[(n<0)|n in x;x[0]+i;n],x}/[x;enlist 0]} 1005
2683

solution:

q){{i:(#:)x;n:x[0]-i;?[(n<0)|n in x;x[0]+i;n],x}/[x;(,)0][0]} each 5 15 25 100 1005
7 24 17 164 2683

better solution (73 characters) naturally working on vector input:

q){{i:count x;n:x[i-1]-i;x,?[(n<0)|n in x;x[i-1]+i;n]}/[max x;enlist 0][x]} 5 15 25 100 1005
7 24 17 164 2683

1

u/gentlegoatfarmer Mar 04 '18

Scala

  object RecamanSequence extends App {
     def a(n: Int): Int = {
        def inner(n: Int): List[Int] = n match {
           case _ if n < 0 => Nil
           case 0 => List(0)
           case _ =>
              val recurResult = inner(n - 1)
              val toCheck = recurResult.head - n
              if (toCheck > 0 && !recurResult.contains(toCheck))
                 toCheck :: recurResult
              else
                 recurResult.head + n :: recurResult
        }
        inner(n).head
     }
     List(5, 15, 25, 100, 1005).foreach(n => println(a(n)))
  }

1

u/Xeonfobia Mar 05 '18 edited Mar 06 '18

Lua 5.2

s = {0}
for i = 1, 1005 do
  if (function() for j = 1, #s do
  if s[j] == s[i] - i then return false end end return true end)() 
  and s[i] > i then 
    s[i + 1] = s[i] - i
  else
    s[i + 1] = s[i] + i 
  end
end
print(s[6], s[16], s[26], s[101], s[1006])
end

2

u/Scara95 Mar 05 '18

A metatable solution. Calculating seq[1005] directly makes you hit recursion limit, but if you force calculation of some intermediate value first there's no problem.

mt = {}
mt.__index = function (seq, n)
	local function contains(seq, val, stop)
		for i = 1, stop do
			if seq[i] == val then
				return true
			end
		end
		return false
	end
	local minus = seq[n-1] - n
	if minus > 0 and not contains(seq, minus, n-1) then
		seq[n] = minus
	else
		seq[n] = seq[n-1] + n
	end
	return seq[n]
end
seq = {}
seq[0] = 0
setmetatable(seq, mt)

print(seq[5], seq[15], seq[25], seq[100])
→ More replies (2)
→ More replies (1)
→ More replies (4)