-6
\$\begingroup\$

Problem

Build an octal calculator that can do the following operations:

  • Add (+)
  • Subtract (-)
  • Multiply (*)
  • Divide (/)
  • Check is prime

Counting in octal is like this with decimal

(Octal)  (Decimal)
0        0
1        1
2        2
3        3
4        4
5        5
6        6 
7        7
10       8
11       9

and so on...

So given input a b c

  • a being a number in octal
  • b being an operator (+, -, *, /) not is prime
  • c being a number in octal

Output the expression evaluated as abc = Output (Is prime)

Test cases:

a=3, b='+', c=3 -> 6 (false)

a=5, b='+', c=5 -> 12 (false)

a=1, b='*', c=5 -> 5 (true)

a=7, b='+', c=6 -> 15 (true)

a=31, b='-', c=11 -> 20 (false)

a=25, b='/', c=3 -> 7 (true)

Rules:

  • The output just needs to contain the answer and a truthy/falsey value of whether it is prime so [4,0] is fine.
  • The inputs will always be a case that outputs an integer (integer division)
  • This is code golf so shortest answer wins

When is an octal number a prime

\$\endgroup\$

closed as unclear what you're asking by programmer5000, Cody Gray, TheLethalCoder, Titus, Toto Jul 25 '17 at 9:07

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Can we do something like [12, 0] or [4, 1] instead of 12 (false) and 4 (true)? \$\endgroup\$ – Stephen Jul 19 '17 at 13:10
  • \$\begingroup\$ Yes, will ammend. \$\endgroup\$ – LiefdeWen Jul 19 '17 at 13:11
  • 7
    \$\begingroup\$ ...as if being octal is a property of numbers... \$\endgroup\$ – Leaky Nun Jul 19 '17 at 13:12
  • 1
    \$\begingroup\$ Also, does division refer to integer division? \$\endgroup\$ – Erik the Outgolfer Jul 19 '17 at 13:14
  • 1
    \$\begingroup\$ @totallyhuman, you have to take 3 seperate strings though. \$\endgroup\$ – LiefdeWen Jul 19 '17 at 14:07
1
\$\begingroup\$

Mathematica, 38 bytes

{a=#@@(#~FromDigits~8&)/@#2,PrimeQ@a}&

Anonymous function. Takes input in the form ... &[Plus, {"7", "12"}] (with both numbers as octal strings and the operator being Plus, Subtract, Times, or Divide) and returns output in the form {17, True} (with the number in decimal.) Did this based on my own, possibly incorrect, interpretation of the rules; please comment if I did something wrong.

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

05AB1E, 13 bytes

‚8ö`³.VDps8B‚

Try it online!

Input format:

c
a
b

Output format:

[Is prime, 'Output']

Note that strings and numbers are equivalent datatypes in 05AB1E.

\$\endgroup\$
  • \$\begingroup\$ Hmm...not sure if valid. \$\endgroup\$ – Erik the Outgolfer Jul 19 '17 at 13:25
  • \$\begingroup\$ @MagicOctopusUrn I don't think it'd behave correctly (also there's ð for ' ), since the operator is designed to work on base-10 ints, while it would have incorrect results with base-8 ints. Also you aren't supposed to check if the base-8 version is prime, but the integer itself before converting to base-8 with 8B. \$\endgroup\$ – Erik the Outgolfer Jul 19 '17 at 14:21
  • \$\begingroup\$ No it still makes the operator work on the base-8 versions of the integers (something it's not supposed to do). Basically it would treat them as base-10 integers. \$\endgroup\$ – Erik the Outgolfer Jul 19 '17 at 14:23
  • \$\begingroup\$ Back to disliking the challenge, get it now though. \$\endgroup\$ – Magic Octopus Urn Jul 19 '17 at 14:25
0
\$\begingroup\$

JavaScript, 82 bytes

(a,b,c,k=m=eval((v=0)+a+b+v+c))=>{while(k-->3)m%k||(v=1);return[m.toString(8),!v]}

JSFiddle

Input is in octal base, output is also in octal base.

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

Jelly, 12 bytes

Dḅ8jVµb8V,ÆP

Try it online!

Uses × for * and ÷ or : for / (approved by OP in chat).

Argument 1:

[a, c] 

Argument 2:

b

Output:

[Output, Is prime]
\$\endgroup\$
0
\$\begingroup\$

Python 2, 89 67 bytes

A whopping -22 bytes thanks to suggestions by Rod.

n=eval(''.join(input()))
print[oct(n),all(n%i for i in range(2,n))]

Try it online!

Takes input as:

['01', '*', '05']

Prints a list like:

['05', True]
\$\endgroup\$
  • 1
    \$\begingroup\$ You can drop both lambdas switching to input()/print to save 2 bytes, also since you are using eval, you don't need to convert to int(,8), you can use octa literals in the string to save 10+ \$\endgroup\$ – Rod Jul 19 '17 at 13:53
  • \$\begingroup\$ 1 is shown prime you need to add x>1and I guess. \$\endgroup\$ – officialaimm Jul 19 '17 at 14:21
0
\$\begingroup\$

PHP, 104 bytes

for(eval("\$i=\$n=octdec($argv[1])$argv[2]octdec($argv[3])|0;");--$i>1&&$n%$i;);echo
decoct($n),_,$i==1;

Run with -nr or try it online.

Output format: Result_1 for primes; Result_ for non-primes.

Output for negative results is faulty; replace the echo part with echo$n<0?-decoct(-$n):decoct($n),_,$i==1; (+17 bytes) to fix.

\$\endgroup\$

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