7
\$\begingroup\$

Your task is to generate the "Primitive and Basic" numbers, which are made like so:

Start with 110. Take the digits of its base-1 representation (110 = 11) and interpret them as base-2 digits. This gives 12 = 110. Now, add the second prime number – 310 – to the result. This will give you 410, which is our first "Primitive and Basic" (PB) number, or PB(1) (you can now see where I got the name from).

Next, take 410 = 1002 and interpret the digits of its base-2 representation as base-3 digits. This gives 1003 = 910. Add the third prime number – 510 – to the result, which gives 1410. That's the second PB number, or PB(2).

You can see where this is going: take 1410 = 1123 and interpret the digits of its base-3 representation as base-4 digits. This gives 1124 = 2210. Add the fourth prime number – 710 – to the result, which gives 2910.

This continues on and on forever.

In general, PB(n) is equal to PB(n-1), converted to base n and from base n+1 to integer, plus the (n+1)th prime.

The first few terms of this sequence are:

4, 14, 29, 52, 87, 132, 185...

You must write a function or program that creates these numbers. It must take a positive integer n as input, and return PB(n).

I/O:

1 -> 4
5 -> 87

Rules and specs:

  • Your program must be able to handle all PB numbers below 231-1.
  • Standard loopholes apply.
  • The input may be 0-indexed or 1-indexed, whichever you prefer.
  • The input is guaranteed to be a valid index.
  • The output must be in base 10.

This is , so shortest code in bytes wins!

Unfortunately, there isn't an OEIS sequence for this. You can propose to OEIS to add this sequence in if you want.

\$\endgroup\$
  • \$\begingroup\$ I find "turn it into base 3" misleading. That sounds like I should convert either 4 or 100 to base 3 which is either 11 or 10201. What you seem to intend is that the representation from the previous base should now be treated as digits in the next base. \$\endgroup\$ – Martin Ender Dec 20 '16 at 9:26
  • \$\begingroup\$ What are we supposed to do? Create a program/function takes takes input and gives the nth PB term? Or are we supposed to generate all terms below 2^31-1? \$\endgroup\$ – Kritixi Lithos Dec 20 '16 at 9:28
  • \$\begingroup\$ Sorry for the misleading thing: your program must be able to handle terms that are up to 2^31-1. It's meant to take an input n and return the output PB(n). \$\endgroup\$ – Qwerp-Derp Dec 20 '16 at 9:29
  • \$\begingroup\$ I just hope all these comments get me the "Blue in the Face" hat \$\endgroup\$ – Kritixi Lithos Dec 20 '16 at 9:30
  • 1
    \$\begingroup\$ I didn't understand the task the first time I read it, so I rephrased it in an attempt to make it clearer. Please feel free to roll back any changes you disagree with. \$\endgroup\$ – Dennis Dec 21 '16 at 7:01
1
\$\begingroup\$

Ruby, 126 bytes

require("prime")
->n{o=4
(2..n).map{|i|j=i+1
r=[]
(r.unshift o%i;o/=i)while o>0
o=r.inject{|s,k|s*j+k}+Prime.first(j)[-1]}
o}
\$\endgroup\$
1
\$\begingroup\$

Jelly, 13 bytes

’ß1¹?bḅ‘+‘ÆN$

Try it online!

How it works

’ß1¹?bḅ‘+‘ÆN$  Main link. Argument: n

’              Decrement; yield n-1.
   ¹?          If n-1 is non-zero:
 ß               Recursively call the main link with argument n-1.
               Else:
  1              Yield 1.
     b         Convert the result to base n.
      ḅ‘       Convert from base n+1 to integer.
         ‘ÆN$  Increment n and yield the (n+1)-th prime.
        +      Add the results to both sides.
\$\endgroup\$
0
\$\begingroup\$

Mathematica, 66 bytes

Fold[IntegerDigits[#,#2-1]~FromDigits~#2+Prime@#2&,4,2~Range~#+1]&

Unnamed function taking a single (1-indexed) integer argument and returning an integer.

Fold[...,4,2~Range~#+1] iterates a function of two arguments, with the first argument beginning at 4 and thereafter set to the output of the previous iteration, while the second argument iterates over the set {3,4,...,#+1} (where # is the original integer argument).

The function in question is IntegerDigits[#,#2-1]~FromDigits~#2+Prime@#2. First, IntegerDigits[#,#2-1] converts the first argument into a list of its digits when written in the previous base; then, ...~FromDigits~#2 converts that list of digits back into an integer, assuming the digits are now in the current base. Finally, the #2th prime is added to the result.

\$\endgroup\$
0
\$\begingroup\$

Perl 6, 95 bytes

{my $b=1;(4,{grep(*.is-prime,2..*)[++$b]+.polymod($b xx*).&{sum $_ Z*($b+1 X**0..*)}}...*)[$_]}

Zero-based indexed.

Explanation:

Since the language's built-in .base and .parse-base methods are string-based, they only work up to base 36. So in order to fulfill the task requirement that large inputs must be supported, the base conversion is done as follows:

  • .polymod($b xx *): Convert the current number to a list representing its digits in base $b.
  • sum $_ Z* ($b+1 X** 0..*): Interpret the resulting digits as a number in base $b+1.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.