6
I'm trying to strip down my python code to be as few lines as possible. The task: count how many square numbers are below 100. Here's my code so far:
m = 0
for x in range(0, 100):
m+=1-(-((-((x+1)**(0.5)-((x+1)**(0.5))//1))//1));
print(m)
That addition statement adds 1 to m (the counter) only when (x+1) is a perfect square. Is it possible to shorten anything? Maybe find a way to print without using a counter -- such as printing a summation in a form such as:
print(sum(min,max,variable,statement))
9
print(9)
works :P – ASCII-only – 2017-04-07T03:33:29.53310To close voters: [tips] are on topic. – Rɪᴋᴇʀ – 2017-04-07T03:34:51.027
I mean, you can get rid of the semicolon on your third line. – Ben Frankel – 2017-04-07T03:35:35.827
1You say you want to count how many square numbers are below 100, but you're counting how many are between 1 and 100 inclusive. Which is it? – Ben Frankel – 2017-04-07T03:42:28.333
4
Have you taken a look at out Python golf tips? In particular, there's some easy gains from removing whitespace and parentheses. I think asking for particular advice for this particular golf is putting the cart before the horse.
– xnor – 2017-04-07T03:45:07.663You could quite easily do
print(int(100**0.5))
... – HyperNeutrino – 2017-04-07T03:52:07.383@Riker, "That addition statement adds 1 to m (the counter) only when (x+1) is a perfect square." – Ben Frankel – 2017-04-07T03:53:58.653
@Ben oh, didnt see that. Sorry. – Rɪᴋᴇʀ – 2017-04-07T03:56:20.183