for(x=1;x<20;++x)
{if(x%2==0) printf("%d divides by 2\n",x);
if(x%3==0) printf("%d divides by 3\n",x);
if(x%3!=0&&x%2!=0)
printf("%d no divides (by 3 or 2)\n",x);
}
On Thu, 29 Oct 2026 08:30:33 +0100, Rosario19 wrote:
for(x=1;x<20;++x)
{if(x%2==0) printf("%d divides by 2\n",x);
if(x%3==0) printf("%d divides by 3\n",x);
if(x%3!=0&&x%2!=0)
printf("%d no divides (by 3 or 2)\n",x);
}
Generalization I call ?bizzfuzz? -- substitute whatever
factors and limit you want:
factors = (2, 3, 5, 7) # factors to be checked in order
limit = 200
for n in range(1, limit + 1) :
i = (list(n % factors[j] == 0 for j in range(len(factors))) + [True]).index(True)
if i < len(factors) :
print("%d is divisible by %d" % (n, factors[i]))
else :
print("%d is not divisible by any of (%s)" % (n, ", ".join(str(f) for f in factors)))
On Thu, 29 Oct 2026 08:30:33 +0100, Rosario19 wrote:
for(x=1;x<20;++x)
{if(x%2==0) printf("%d divides by 2\n",x);
if(x%3==0) printf("%d divides by 3\n",x);
if(x%3!=0&&x%2!=0)
printf("%d no divides (by 3 or 2)\n",x);
}
Generalization I call ?bizzfuzz? -- substitute whatever
factors and limit you want:
factors = (2, 3, 5, 7) # factors to be checked in order
limit = 200
for n in range(1, limit + 1) :
i = (list(n % factors[j] == 0 for j in range(len(factors))) + [True]).index(True)
if i < len(factors) :
print("%d is divisible by %d" % (n, factors[i]))
else :
print("%d is not divisible by any of (%s)" % (n, ", ".join(str(f) for f in factors)))
#end if
#end for
On Thu, 29 Oct 2026 08:30:33 +0100, Rosario19 wrote:[SNIP]
for(x=1;x<20;++x)
{if(x%2==0) printf("%d divides by 2\n",x);
if(x%3==0) printf("%d divides by 3\n",x);
if(x%3!=0&&x%2!=0)
printf("%d no divides (by 3 or 2)\n",x);
}
Generalization I call ?bizzfuzz? -- substitute whatever
factors and limit you want:
factors = (2, 3, 5, 7) # factors to be checked in order
limit = 200
for n in range(1, limit + 1) :
[SNIP]
comp.lang.python is down the hall, just past the water cooler, on
the left.
On Thu, 29 Oct 2026 08:30:33 +0100, Rosario19 wrote:
for(x=1;x<20;++x)
{if(x%2==0) printf("%d divides by 2\n",x);
if(x%3==0) printf("%d divides by 3\n",x);
if(x%3!=0&&x%2!=0)
printf("%d no divides (by 3 or 2)\n",x);
}
Generalization I call ?bizzfuzz? -- substitute whatever
factors and limit you want:
factors = (2, 3, 5, 7) # factors to be checked in order
limit = 200
for n in range(1, limit + 1) :
i = (list(n % factors[j] == 0 for j in range(len(factors))) + [True]).index(True)
if i < len(factors) :
print("%d is divisible by %d" % (n, factors[i]))
else :
print("%d is not divisible by any of (%s)" % (n, ", ".join(str(f) for f in factors)))
#end if
#end for
That output becomes:
167 is not divisible by any of (2,3,5,7)
168 is divisible by 2
168 is divisible by 3
168 is divisible by 7
169 is not divisible by any of (2,3,5,7)
Sizes (with hard tabs and no comments):
Your Python: 340 characters
Mine: 222 (as above; scripting)
C: 393 (not shown; not elegant)
Mine: 314 (not shown; systems lang)
On Fri, 30 Oct 2026 11:47:59 +0000, bartc wrote:
That output becomes:
167 is not divisible by any of (2,3,5,7)
168 is divisible by 2
168 is divisible by 3
168 is divisible by 7
169 is not divisible by any of (2,3,5,7)
Sizes (with hard tabs and no comments):
Your Python: 340 characters
Mine: 222 (as above; scripting)
C: 393 (not shown; not elegant)
Mine: 314 (not shown; systems lang)
The APL one is 80 characters
Is more long the string to write that the code to execute...
On 31/10/2026 10:39, Rosario19 wrote:
On Fri, 30 Oct 2026 11:47:59 +0000, bartc wrote:
That output becomes:
167 is not divisible by any of (2,3,5,7)
168 is divisible by 2
168 is divisible by 3
168 is divisible by 7
169 is not divisible by any of (2,3,5,7)
Sizes (with hard tabs and no comments):
Your Python: 340 characters
Mine: 222 (as above; scripting)
C: 393 (not shown; not elegant)
Mine: 314 (not shown; systems lang)
The APL one is 80 characters
That's not quite right:
* You're using 2,3,7 not 2,3,5,7, which would add 2 characters
* You're doing 20 iterations not 200 (adds 1 more character!)
On 31/10/2026 10:39, Rosario19 wrote:
On Fri, 30 Oct 2026 11:47:59 +0000, bartcÿ wrote:
That output becomes:
ÿÿ 167 is not divisible by any of (2,3,5,7)
ÿÿ 168 is divisible by 2
ÿÿ 168 is divisible by 3
ÿÿ 168 is divisible by 7
ÿÿ 169 is not divisible by any of (2,3,5,7)
Sizes (with hard tabs and no comments):
ÿ Your Python:ÿ 340 characters
ÿ Mine:ÿÿÿÿÿÿÿÿ 222ÿÿÿ (as above; scripting)
ÿ C:ÿÿÿÿÿÿÿÿÿÿÿ 393ÿÿÿ (not shown; not elegant)
ÿ Mine:ÿÿÿÿÿÿÿÿ 314ÿÿÿ (not shown; systems lang)
The APL one is 80 characters
That's not quite right:
* You're using 2,3,7 not 2,3,5,7, which would add 2 characters
* You're doing 20 iterations not 200 (adds 1 more character!)
* You missed out 'any of ' (adds 6 characters, however your output seems
ÿ to have unnecessary space so perhaps that can be trimmed)
Is more long the string to write that the code to execute...
The point about the version I posted is that it does the correct task
but much cleanly than Python, which had lots of unnecessary
complications. So anyone could follow it.
But I modified my version to be compact, and it looks like this:
ÿ f:=(2,3,5,7)
ÿ for n to 20 do
ÿÿ d::=()
ÿÿ for x in f when n rem x=0 do d&:=x od
ÿÿ ?n,(d|"is divisible by"|"is not divisible by"),(d|d|f)
ÿ od
This is 134 characters (131 without those indents). It suffers from
named symbols such as 'rem' needing 5 characters rather than 1.
Note that your APL (with '5' added), because it uses Unicode, takes up
105 bytes; my version is still 131/134 bytes while still just about
being understandable.
On Sat, 31 Oct 2026 12:21:13 +0000, bartc <bc@freeuk.com> wrote:
On 31/10/2026 10:39, Rosario19 wrote:
On Fri, 30 Oct 2026 11:47:59 +0000, bartc wrote:
That output becomes:
167 is not divisible by any of (2,3,5,7)
168 is divisible by 2
168 is divisible by 3
168 is divisible by 7
169 is not divisible by any of (2,3,5,7)
Sizes (with hard tabs and no comments):
Your Python: 340 characters
Mine: 222 (as above; scripting)
C: 393 (not shown; not elegant)
Mine: 314 (not shown; systems lang)
The APL one is 80 characters
That's not quite right:
* You're using 2,3,7 not 2,3,5,7, which would add 2 characters
* You're doing 20 iterations not 200 (adds 1 more character!)
code I posted is 2 input one left (the list 2 3 7) and one in the
right (the number 20) and the APL function in the middle.
80 charaters refer only for APL function.
So the number of characters of the function + the input is more than
80. It is possible change the input [input left the list 2 3 5 7,
input right the integer 200] for have this
2 3 5 7{a???{0=?k?a/?0=a|?:???,' is not divisible by ',a????,' is divisible by ',k}???}200
1 is not divisible by 2 3 5 7
2 is divisible by 2
1 is not divisible by 2 3 5 7
2 is divisible by 2
I suggested you can save characters (2 or 4?) by trimming those strings,
as there is extra white space here.
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 4 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 190:54:39 |
| Calls: | 222 |
| Files: | 21,513 |
| Messages: | 85,912 |