Skip to main content
Stack Overflow
  1. About
  2. For Teams
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Return to Answer

jsbin preview
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161

##JavaScript (SpiderMonkey) - 118 chars## This version accepts input from stdin and passes the bonus test cases

r=readline()
for(t=0,i=-r;i<r;i++)if(i%2){for(s='',j=-r;j<r;j++){t+=q=i*i+j*j<r*r
s+=q?'*':' '}print(s)}print(t*2/r/r)

Usage:cat 10 | js thisfile.js -- jsbin preview adds an alias for print/readline so you can view in browser

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 118 chars## This version accepts input from stdin and passes the bonus test cases

r=readline()
for(t=0,i=-r;i<r;i++)if(i%2){for(s='',j=-r;j<r;j++){t+=q=i*i+j*j<r*r
s+=q?'*':' '}print(s)}print(t*2/r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 118 chars## This version accepts input from stdin and passes the bonus test cases

r=readline()
for(t=0,i=-r;i<r;i++)if(i%2){for(s='',j=-r;j<r;j++){t+=q=i*i+j*j<r*r
s+=q?'*':' '}print(s)}print(t*2/r/r)

Usage:cat 10 | js thisfile.js -- jsbin preview adds an alias for print/readline so you can view in browser

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');
118 by simplifying problem
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161

##JavaScript (SpiderMonkey) - 157118 chars## This version accepts input from stdin and passes the bonus test cases

r=readline()
p=print
a=Array
m=Math
for(t=0,i=-r+1;i<r;i++r;i<r;i++)if(i%2){t+=j=m.floor(m.sqrtfor(r*rs='',j=-i*i)r;j<r;j++)*2+1{t+=q=i*i+j*j<r*r
p(a(m.round(r-j/2)).join(s+=q?'*':' ')+a(j).join}print('*')s)}pprint(t*2/r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 157 chars## This version accepts input from stdin and passes the test cases

r=readline()
p=print
a=Array
m=Math
for(t=0,i=-r+1;i<r;i++)if(i%2){t+=j=m.floor(m.sqrt(r*r-i*i))*2+1
p(a(m.round(r-j/2)).join(' ')+a(j).join('*'))}p(t*2/r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 118 chars## This version accepts input from stdin and passes the bonus test cases

r=readline()
for(t=0,i=-r;i<r;i++)if(i%2){for(s='',j=-r;j<r;j++){t+=q=i*i+j*j<r*r
s+=q?'*':' '}print(s)}print(t*2/r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');
deleted 2 characters in body
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161

##JavaScript (SpiderMonkey) - 159157 chars## This version accepts input from stdin and passes the test cases

r=readline()
p=print
a=Array
m=Math
for(t=0,i=-r+1;i<r;i++)if(i%2){t+=j=m.floor(m.sqrt(r*r-i*i))*2+1
p(a(m.round(r-j/2)).join(' ')+a(j).join('*'))}p(t*2/(r*r)r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 159 chars## This version accepts input from stdin and passes the test cases

r=readline()
p=print
a=Array
m=Math
for(t=0,i=-r+1;i<r;i++)if(i%2){t+=j=m.floor(m.sqrt(r*r-i*i))*2+1
p(a(m.round(r-j/2)).join(' ')+a(j).join('*'))}p(t*2/(r*r))

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');

##JavaScript (SpiderMonkey) - 157 chars## This version accepts input from stdin and passes the test cases

r=readline()
p=print
a=Array
m=Math
for(t=0,i=-r+1;i<r;i++)if(i%2){t+=j=m.floor(m.sqrt(r*r-i*i))*2+1
p(a(m.round(r-j/2)).join(' ')+a(j).join('*'))}p(t*2/r/r)

Javascript: (削除) 213 (削除ここまで) 163

----------

Updated

r=10;m=Math;a=Array;t=0;l=document;for(i=-r;i<r;i+=2){w=m.floor(m.sqrt(r*r-i*i)*2);t+=w*2;l.writeln(a(m.round(r-w/2)).join(' ')+a(w).join('*'));}l.writeln(t/(r*r))

Nobody said it had to render correctly in the browser - just the output. As such I've removed the pre tags and optimised it further. To view the output you need to view generated source or set your stylesheet accordingly. Pi is less accurate this way, but it's now to spec.


r=10;m=Math;a=Array;t=0;s='';for(i=-r;i<r;i++){w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);t+=w;if(i%2){z=a(m.round(r-w/2)).join(' ')+a(w).join('*');s+=z+'\n';}}document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>')

Unminified:

r=10;
m=Math;
a=Array;
t=0;
s='';
for(i=-r;i<r;i++){
 w=m.floor((m.sqrt(m.pow(r,2)-m.pow(i,2)))*2);
 t+=w;
 if(i%2){
 z=a(m.round(r-w/2)).join(' ')+a(w).join('*');
 s+=z+'\n';
 }
}
document.write('<pre>'+(s+(t/m.pow(r,2)))+'</pre>');
159 + test cases
Source Link
gnarf
  • 106.6k
  • 26
  • 129
  • 161
Loading
Optimised solution; deleted 1 characters in body; added 52 characters in body
Source Link
Iain Fraser
  • 6.7k
  • 8
  • 45
  • 68
Loading
Post Made Community Wiki
Source Link
Iain Fraser
  • 6.7k
  • 8
  • 45
  • 68
Loading

AltStyle によって変換されたページ (->オリジナル) /