# Convert radicals to mixed & entire radicals and to real numbers [closed]

## Challenge

The goal of this challenge is to take a radical and convert it to a reduced and entire radical, and a real number. You do not have to convert it to all 3, however, you will get bonus points for converting it to 2 or 3 of them. You will also get bonus points if your code works not just with square roots. How the input works is your choice. You don't have to parse the input so you could just have it as a function. If your unable to output the root symbol (√) then you could output it as sqrt(x) or cuberoot(x). You could also use a superscript (²√) for the index of the radical.

## Rules

• If you just output one of the 3 options, then you can't just output it without converting it (ie: if the input is √125 then you can't just output √125 back).
• The entire radical must have no co-efficient.
• The mixed radical must have a coefficient (if there is no coefficient, then it should be 1).
• The real number (or result of the radical) should have as many decimals as possible.

## Example

Here's an example output for the square root of 125 (√125):

Entire radical: √125
Real number: 11.18033988749895


## Scoring

• 10% off your score for outputting 2 of either an entire radical, mixed radical, or real number.
• 30% off your score for working with a radical with any index

## Winner

The person with the shortest code length wins. Good luck!

• What is the first enumerated rule supposed to mean? Specifically: 1. Does it imply that the solution must accept input in the form of an entire radical? 2. Is that the only input form which must be supported? 3. If a program only outputs entire radicals, and an entire radical is supplied as input, what should the program do? Not output anything? – Peter Taylor Jan 15 '14 at 8:42
• You may also want to specify, that the coefficient for mixed radicals is not always 1 but the maximum integer value. – Howard Jan 15 '14 at 9:30

# APL, 77 × .7² = 37.7

{(a b)←×⌿(∪p)*[1]c,⍪e-⍺×c←⌊⍺÷⍨e←∪⍦p←π⍵⋄q←⍺/⍨⍺≠2⋄∊¨⍕¨¨⍪(q'√'⍵)(a'×'q'√'b),⍺√⍵}


Nars2000. Could be golfed some more, for instance by printing worse output, but I'm being lazy.
Also, pretty output is pretty.

Ungolfed

{
p←π⍵                       ⍝ prime factors of the radical argument ⍵
u←∪p                       ⍝ unique prime factors
e←∪⍦u                      ⍝ exponents of those factors (so that ⍵=u*e)
r←⌊e÷⍺                     ⍝ u's exponents for the reduced radical factor
i←⍺/⍨⍺≠2                   ⍝ index, unless it's 2 (AH THE DECADENCE, I CAN WASTE SO MUCH)
o←(i'√'⍵)(f'×'i'√'a),⍺√⍵   ⍝ the three expressions to output
∊¨⍕¨¨⍪o                    ⍝ return them as a table without spaces
}


Examples

     2 f 125
√125
5×√5
11.18033989
2 f 1440
√1440
12×√10
37.94733192
3 f 1440
3√1440
2×3√180
11.29243235
2 f 987654321
√987654321
51×√379721
31426.96805

• Would it not cost fewer bytes to only do two tasks? – Leaky Nun May 25 '16 at 23:20

## C (314)

n,d;main(i){scanf("%d %d",&n,&d);int val[d];for(;pow(i,n)<=d;i++)val[i]=pow(i,n);printf("Entire radical: %drt(%d)\n",n,d);for(i--;i>0;i--)if(d%val[i]==0){printf("Mixed radical: %d*%drt(%d)\n",i,n,d/val[i]);break;}double v,x=1.5,a=d;do{v=(a/pow(x,n-1)-x)/n;x+=v;}while(abs(v)>=1E-9);printf("Real number: %lf\n",x);}


Here it is with whitespace.

n,d;
main(i){
scanf("%d %d",&n,&d);
int val[d];

for(;pow(i,n)<=d;i++)
val[i]=pow(i,n);

for (i--;i>0;i--)
if (d%val[i]==0){
printf("Mixed Radical: %d*%drt(%d)\n", i, n, d/val[i]);
break;}

double v,x=1.5,a=d;
do{
v=(a/pow(x,n-1)-x)/n;
x+=v;
}while(abs(v)>=1E-9);

printf("Real number: %lf\n", x);
}


Input:

2 125


Output:

Entire radical: 2rt(125)
Real number: 11.180340


Input:

3 125


Output:

Entire radical: 3rt(125)
Real number: 5.000000


This isn't perfect, the root calculation will not work for all input values, and the chosen guess value is not random. It takes the whole value of the radical as input (see above).

# C - 222 × 70% × 70% ≆ 108.78

p,c=1,i,n=2,t;main(){scanf("%d %d",&i,&p);printf("Entire radical: rt%d(%d)\nReal number: %.16
lf\n",i,p,pow(p,1.0l/i));for(;n++<p;t=0){for(;!(p%n);t++)p/=n;c*=pow(n,t/i);p*=pow(n,t%i);}pr
intf("Mixed root: %drt%d(%d)",c,i,p);}


Has some problems with rounding, but is otherwise fine.

# Ruby, 199 chars * 70% * 70% = 97.51

I'm not good at mathstuffs. :P

a,b=gets.split.map &:to_i
c=b
[*2..o=b].map{|x|x**a}.map{|x|o=c/=x if c%x==0}until o==b
Real number: #{b**(1.0/a)}"


I particularly like line 3. It's supposed to keep dividing out squares until it can't divide anymore. How I did it is that o is set to b in the beginning (conveniently tucking it into the range expression), and if a square can be divided out, o is set to the result (also conveniently only adding 2 more characters). Then, I used until o==b, so that it keeps going until o didn't change (meaning no squares could be divided out).

More explanations coming soon....

Sample runs:

c:\a\ruby>radicalstuffs
2 100
Real number: 10.0

2 125
Real number: 11.180339887498949

2 100
Real number: 10.0

5 32
Real number: 2.0

3 27
Real number: 3.0

2 99
Real number: 9.9498743710662

2 98

julia> f(n)="$(sqrt(n)) √$n"