Tag Info

New answers tagged

-1

Python - 48 chars Uses SymPy (or is it Sympy?) from sympy import * solve(input(), symbols('x')) Here's how.


0

Mathematica 27 f@x_:=Nest[(#+x/#)/2&,1.,9]


0

Postscript 94 89 Babylonian method. Iterates until successive values are equal. s{dup 2 div{2 copy div 1 index add .5 mul 2 copy eq{exit}if exch pop}loop pop exch pop}def Commented: /s{ dup 2 div % S x { % S x 2 copy div % S x S/x 1 index add .5 mul % S x (S/x+x)/2 2 copy eq { exit } if % S x (S/x+x)/2 exch ...


1

Mathematica 65 64 c=Column;c[c@Table[n~Binomial~k,{n,0,p},{k,0,n}]~Table~{p,0,#}]& Usage c=Column;c[c@Table[n~Binomial~k,{n,0,p},{k,0,n}]~Table~{p,0,#}]&@5


1

Mathematica 36 InputForm@Expand[(#1 a + #2 b )^#3] & Usage InputForm@Expand[(#1 a + #2 b )^#3] &[5, .5, 6] Result: 15625*a^6 +9375.*a^5*b +2343.75*a^4*b^2 +312.5*a^3*b^3 +23.4375*a^2*b^4 + 0.9375*a*b^5 + 0.015625*b^6


1

Python, 110 l=1 exec"print;n=1;r=0;exec\"c=0;exec'print n,;n*=r-c;c+=1;n/=c;'*r;print n;r+=1;n=n*l/r-n;\"*l;l+=1;"*input() Original, ungolfed solution: for layer in range(1, input() + 1): print n = 1 for row in range(1, layer + 1): for col in range(1, row): print n, n *= row - col n /= col ...


1

Mathematica (112) For[j = 0, j < 11, j++, For[i = 0, i <= j, i++, Print[Row[Array[Binomial[i, # - 1] &, i + 1], " "]]]; Print[""]] Older method(166): b = "stdout"; For[k = 0, k < 11, k++, For[i = 0, i <= k, i++, For[j = 0, j <= i, j++, WriteString[b, Binomial[i, j], " "]]; WriteString[b, "\n"]]; WriteString[b, "\n"]] The following ...


0

Python (134 128) Factorials are probably more space-efficient, but I wanted to be fancy. for n in range(1,input()+1): a=r=1. while a: b=a;j=1.;a*=(n-r)/r while b:print int(b),;b*=(r-j)/j;j+=1 print;r+=1 print Uses the ratio property of Pascal's Triangle. e.x. 1 4 6 4 1, 1:4, 4:6, 6:4, 4:1 = 1:4, 2:3, 3:2, 4:1 for n in range(1,input()+1): ...


0

F# 153 chars 156 bytes let a,b,n=1.,2.,3 let rec C k=if k=0 then 1 else (n-k+1)*C(k-1)/k List.iter(fun i->printf"%+ga^%ib^%i"(pown a (n-i)*pown b i*float(C i)) (n-i) i)[0..n]


0

C (C99), 223 chars #include <stdio.h> #include <math.h> int f(int n,int k){ return k==0?1:(n*f(n-1,k-1))/k; } int main(void){ float a,b;int n; scanf("%f%f%d",&a,&b,&n); for(int k=0;k<=n;k++) printf("%d*(%fa)^%d*(%fb)^%d+",f(n,k),a,n-k,b,k); puts("0"); } Input: 1 1 2 Output: ...


0

APL, 19 15 characters A bit late to the party, perhaps? {⍪{⍵!⍨⍳⍵+1}¨⍳⍵} It doesn't beat the J entry, though. This assumes that the index origin (⎕IO) is set to 0. Unfortunately, with an index origin of 1, we need 25 18 characters: {⍪{⍵!⍨0,⍳⍵}¨1-⍨⍳⍵} There are two ⍨s in the code to express my frustration. Demo: {⍪{⍵!⍨⍳⍵+1}¨⍳⍵}5 1 1 1 1 2 1 1 3 ...


0

Python, 67 chars, complex and distance-based, and no false positives ... S=lambda A:len(set(A))-1==len(set([abs(K-J)for K in A for J in A])) (I hope;-). The sqrt in abs() may cause some roundoff problems, but probably not - abs((K-J)**2) is more reliable but compresses domain as numbers can approach to within a few digits of double precision limits. ...


1

Python, 66 Improving paperhorse's answer from 76 to 66: def U(A):c=sum(A)/4;d=A[0]-c;return{d+c,c-d,d*1j+c,c-d*1j}==set(A)


1

Python, 64 Using some of boothby's ideas. a,b=map(int,raw_input().split()) r=1;exec"r=eval('a*'*r+'1');"*b The result is stored in r.


1

Common Lisp 85 chars (lambda(b c)(let((r b)u)(dotimes(c c r)(setf u 1 r(dotimes(c b u)(setf u(* u r))))))) I tried doing the multiplications through repeated addition, but it was way more than 5 characters. Same thing with macrolets, the declarations were not worth the gains. Another solution, inspired by boothby's python solution. It's 1 character ...


0

APL (36) {x t←⍺⍵⋄0{t>i←(x*⍵)÷!⍵:⍺⋄∇/⍺⍵+i 1}0} The left argument is the number and the right argument is the tolerance, i.e. 4 {x t←⍺⍵⋄0{t>i←(x*⍵)÷!⍵:⍺⋄∇/⍺⍵+i 1}0} 0.00001 54.59814722 It uses a power series expansion. Explanation: x t←⍺⍵: store the left argument (x) in x and the right argument (tolerance) in t 0{...}0: iterator ...


1

Mathematica 65 80 69 66 Checks that the number of distinct inter-point distances (not including distance from a point to itself) is 2 and the shorter of the two is not 0. h = Length@# == 2 \[And] Min@# != 0 &[Union[EuclideanDistance @@@ Subsets[#, {2}]]] &; Usage h@{{0, 0}, {0, 1}, {1, 1}, {1, 0}} (*standard square *) h@{{0, 0}, {2, 1}, ...


0

Python2, 95 characters, Non-recursive A bit more verbose than the other python solutions but it's non-recursive so it doesn't hit cpython's recursion limit: from itertools import* f=lambda n:next(i for i in count()if sum(1>i%(j+1)for j in range(i))==n)


0

C++, 87 characters int a(int d){int k=0,r,i;for(;r!=d;k++)for(i=2,r=1;i<=k;i++)if(!(k%i))r++;return k-1;}


2

Mathematica, 90 f[x_,p_]:=Piecewise[{{Sum[x^i/i!,{i,0,50}],x>0},{1/Sum[(-x)^i/i!,{i,0,50}],x<0},{1,x==0}}] I also ignore the precision p and assume that 20 decimal points will be the most accuracy desired. It also works for negative values of x! To be worked on more later. Python, 88 def E(x,p): t=n=s=1.;y=x if x<0:y=-x while ...


0

JavaScript, 50 Chars Given that javascript has variable args support, this will work with the input e(4,0.00001). Otherwise, it would be 52 chars. function e(x){for(t=n=s=1;t*=x/n++;s+=t);return s} ideone If it is going to factor in the precision as the problem states, it can be done in 56 chars. function e(x,p){for(t=n=s=1;(t*=x/n++)>p;s+=t);return ...


1

Python, 59 57 chars def E(x,p): t=n=s=1. while t:t*=x/n;n+=1;s+=t return s Uses the power series expansion of e^x. Ignores p and just calculates the result to full double precision.


0

C, 88 69 double e(int e, double l) {double a,p=1,i=0,f=1,r=1;while ((a=(p*=e)/(f*=++i))>l) r+=a;return r;} double e(int x, double l) {double i=0,t=1,r=1;while (t*=x/++i) r+=t;return r;} call e. if you change the first argument to e from int to double, it will even work for floating point exponents. Code counted without any white space or ...


-1

wc -c; ln(0)=-inf Here's the program: 0 characters. Save as log.wc, run as echo 123|wc -c log.wc


0

Smalltalk Squeak 4.x flavour, 91 char in the form of a block: [:p||b|b:=Bag new.p do:[:x|p do:[:y|b add:(x dist:y)]].b valuesAndCounts asSet={4. 8}asSet] Test it with: { "Example squares:" {0@ 0. 0@ 1. 1@ 1. 1@ 0}. " standard square" {0@ 0. 2@ 1. 3@ -1. 1@ -2}. " non-axis-aligned square" {0@ 0. 1@ 1. 0@ 1. 1@ 0}. " different order" "Example ...


1

186 bytes (39256 points?) <script>function m(a,j,l){return l?m(a,j,--l)*(1-i)+m(a,j+1,l)*i:a[j]}P='<svg><path stroke=red d=' for(i=0;i<=1;i+=1/64)P+='ML'[i&&1]+m(X,0,n)+','+m(Y,0,n) document.write(P+'>')</script> Usage is exactly the same as Peter Taylor's solution. Also credits to Peter for the svg path approach (I ...


2

Scala - ln(46) = 3.82864... print(math.log(args(0).toDouble)/math.log(46)) scala log.scala 12.34 -> 0.6563283834264416


2

BrainF***, 276000 ++++++++++++++++++++++++[->++<].--.++...... ... followed by just under e276200 of any byte except [, ], . or ,. (When you present the score like that, it doesn't even seem so bad!)



Top 50 recent answers are included