Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
The challenge involves mathematics in some central way. Also consider using more specific tags, listed in the tag wiki info.
3
votes
Calculate the cube root of a number
Javascript (55)
function f(n){for(i=x=99;i--;)x=(2*x+n/x/x)/3;return x}
BONUS, General formulation for all roots
function f(n,p){for(i=x=99;i--;)x=x-(x-n/Math.pow(x,p-1))/p;return x}
For cube root, …
4
votes
Solve a System of Linear Equations
Javascript (284 181) - Gauss Elimination method
function f(A){l=A.length;d=1;for(i=0;i+1;i+=d){v=A[i][i];for(k=0;k<l+1;k++)A[i][k]/=v;for(j=i+d;A[j];j+=d)for(k=0,w=A[j][i];k<l+1;k++)A[j][k]-=w*A[i][k …
1
vote
Calculate all the squares up to x using only addition and subtraction
Javascript 47
function f(n,a){return a[n]=n?f(n-1,a)+n+n-1:0}
r=[];f(12,r);console.log(r) returns :
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144]
0
votes
Shortest Program to Solve a Quartic Equation
Javascript/ES6 148
Since only a solution is required, I used a Newton solver.
f=s=>{a=s.split(' ');for(x=i=0;i++<1e5;x-=y)z=x*x,y=(a[0]*z*z+a[1]*x*z+a[2]*z+a[3]*x+ +a[4])/(4*a[0]*z*x+3*a[1]*z+2*a[ …
2
votes
Divide Two Numbers Using Long Division
Brainfuck
works only for numbers between 1 and 9
Do not fit the rules, so I don't expect to win but answers in brainfuck are always awesome.
++++++++>,>,<<[>------>------<<-]>[->-[>+>>]>[+[-<+>]>+> …
2
votes
Write a word equation solver
JavaScript 138
for(s=prompt(p='1');eval(p.replace('=','!='));)for(p=s,i=64;i++<90;)p=p.replace(new RegExp(String.fromCharCode(i),'g'),10*Math.random()|0)
Random bruteforce.
Can take a while (my bes …
4
votes
Raise to the Power
JS (ES6), 103 bytes
t=(x,m,f)=>{for(r=i=s=u=1;i<1<<7+f;r+=s/(u=i++*(f?1:u)))s*=m;return r};e=(a,b)=>t(b,t(a,1-1/a,9)*b-b,0)
Examples :
e(1.234,5.678) = 3.299798925315965
e(4.5,4.5) = 869.87392 …
10
votes
Shortest Minmod Function
JavaScript (ES6), 39 bytes
a=>a.reduce((p,c)=>p*c>0?p*p>c*c?c:p:0)
14
votes
Calculate the inverse of factorial
Only elementary math functions are used: ln, pow, exponential
x=9;n=prompt(M=Math);for(i=1e4;i--;)x+=(n/M.exp(-x)/M.pow(x,x-.5)/2.5066/(1+1/12/x+1/288/x/x)-1)/M.log(x);alert(x-1)
Basically, I coded a …
292
votes
Write a program that makes 2 + 2 = 5
C
Pretty cheap trick but I'm sure I will trap the most of you.
int main() {
int a = 2 + 2; …
6
votes
Find i^n, given n
JavaScript (ES6) 29-5 = 24
Supports negative power.
f=n=>(n&2?'-':'')+(n&1?'i':1)
ES5 :
function f(n){return (n&2?'-':'')+(n&1?'i':1)}
2
votes
Closest fraction
JavaScript, 131
With fat arrow notation and eval calls :
m=>{for(e=eval,n=e(m),i=p=0,q=1;++i</\d+$/.exec(m);)if(n*i>(f=n*i|0))g=f+1,p=f/i>e(p)?f+'/'+i:p,q=g/i<e(q)?g+'/'+i:q;return p+' '+q}
The …