3
\$\begingroup\$

I'd like to shorten my answer below by applying this tio.run technique from this C example.

Here's their C example

Code

r,a;f(x){for(a=r=x*x;a--;)r-=hypot(a%x+1,a/x)>x;x=4*r+1;}

Footer

main(){

printf("%d\n",f(1));
printf("%d\n",f(2));
printf("%d\n",f(3));
printf("%d\n",f(10));
printf("%d\n",f(1000));
printf("%d\n",f(2000));
}

but I don't understand how the result is returned from the f() function?

Here's my C code below and I'd like to remove the main, return, printf and just reduce it to the bare minimum bytes using their footer technique etc.,... also, @qwr noted

I think meta consensus is that you can write a function instead of a whole program.

Thanks.

C (gcc), 116 bytes

a,x,y;c(r){for(x=-r;x<=r;x++)for(y=-r;y<=r;a+=x*x+y*y<=r*r,y++);return a;}main(r){scanf("%u",&r);printf("%d",c(r));}

How it works

This inefficient method draws a square around the circle then iterates through all lattice points \$(x,y)\$ within it and checking if \$x^2+y^2 \le r^2 \$.

Try it online!

\$\endgroup\$
2
  • 2
    \$\begingroup\$ codegolf.stackexchange.com/a/106067/85334 \$\endgroup\$ Jan 13 at 1:14
  • \$\begingroup\$ Seems any return variable will do, so it does not have to match the argument passed to the function, in my case r. Here's a sample c,x,y;f(r){c=0;for(x=-r;x<=r;x++){for(y=-r;y<=r;y++){if(x*x+y*y<=r*r){c++;}}}x=c;} and notice the x=c; returns the value, so doing r=c; was not required. \$\endgroup\$
    – vengy
    Jan 13 at 1:25

2 Answers 2

3
\$\begingroup\$

As stated here, a function without a return's return value is undefined. It looks to be in the case of what TIO and many others are using, the return value is the top variable on the stack. See how,

int f(int x)
{
    int a=x;
}

and

int a;int g(int x)
{
    a=x+1;
    x=0;
}

return the input x and x+1 respectively. TIO

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

@ceilingcat reduced it from 116 to 68 bytes.

C (gcc), 68 bytes

a,x,y;c(r){a=0;for(x=~r;x++<r;)for(y=~r;y++<r;)a+=x*x+y*y<=r*r;x=a;}

Try it online!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .