DEV Community

Discussion on: Daily Challenge #13 - Twice Linear

Collapse
 
listnux profile image
ListNUX

Not too good with maths, hope I understood well. Here is a (very inefficient, so I added a constraint to the max num to give) attempt in bash:

#!/bin/bash

declare -a numbersarr
numbersarr[0]=1

echo "Array position \"n\""
read -r myn
# make sure we are given a number and the number is not too large
re="^[0-9]+$"
if ! [[ $myn =~ $re ]] || [ $myn -gt 100 ]; then
  echo "not a number or number too big" >&2; exit 1
fi

arrl=1

while [ $arrl -le $myn ]; do
  for i in "${numbersarr[@]}"; do
    let x=2\*i+1
    let y=3\*i+1
    numbersarr+=( $x $y )
  done
  sorted_unique=($(echo "${numbersarr[@]}" |xargs -n1 | sort -gu | xargs))
  arrl="${#sorted_unique[@]}"
done

# declare -p sorted_unique
echo " >> item in pos ${myn}: "${sorted_unique[$myn]}