# prime - Page 3

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
•  prime
• George Mpouras
• 10-15-2014

## Re: prime

rweikusat@mobileactivedefense.com says...

Ok, its making more sense, thanks.  Is it correct to say then that two statements separated
by a comma, behave as if they are one statement?  At least in the sense that a modifier like
"unless" or "if" will apply to both of them?  If so, I don't like it from a readability
standpoint at least without some clarifying parentheses because its non-obvious that the
"unless" will bind to both the statements and not just to the "last" statement.

John Black

## Re: prime

You're confusing statements and expressions here: The 'comma operator'
is a binary operator taking two expressions as arguments and the whole
construct is 'a simple statement', an expression evaluated because of
its side effects. And the statement modifier applies to the statement
and not its component expressions. This is really no different from

\$a += \$b if \$c < 34;

\$a += \$b is a simple statement (based on another binary operator) and it
is conditionally executed because of the attached statement
modifier. It's not something like

\$a += (\$c < 34 ? \$b : die("Falling over the edge of the world! Aaaargh!"));

## Re: prime

rweikusat@mobileactivedefense.com says...

Well it appears different because \$a and \$b are just scalars whereas "\$N=0" and "last" look
more like statements in their own right, than simple components of a statement.  It wouldn't
make sense to write \$N=0 += last.  But I understand now.  Thanks for the explanation.

John Black

## Re: prime

It likely wouldn't do what was intended because the RHS of the
assignment operator is evaluated first, hence, the last would terminate
the loop before the \$N=0 was executed, but grammatically, this is
perfectly legitimate. It's arguably somewhat weird that the loop control
verbs count as expressions in Perl but frequently useful (as in this
case).

## Re: prime

On 20.10.14 23:52, Rainer Weikusat wrote:

In many cases, there is the expedient of putting parentheses,
as in

(\$a += \$b) if \$c < 34;

which might or might not be seen as less confusing than invoking
Perl's grammar. The parentheses do show the borders, which might
be all that counts.

## Re: prime

For this particular example, the assumption that the statement modifier
is not a statement modifier but an operator just doesn't make any
sense: If not \$b, what else is it going to add?

I used to be in favour of 'if in doubt, add redundant ()s' but I'm not
anymore: C isn't the be-all and end-all of all programming language and
using some subset(!) aka 'pidgin C' to write code in any sufficiently
similar language just makes things needlessly confusing for people whose
'undocumented, native C dialect' differs from the 'undocumented, native
C-dialect' of the code author: I'm interested in using the features
offered to be by \$programming_language productively, not in (endlessly)
documenting that "I had to learn writing Math and if this ain't
sufficient for life, something is wrong with that and it has to adapt"
(because I can't/ won't).

## Re: prime

On 22.10.14 15:44, Rainer Weikusat wrote:

Whether or not "\$b if \$c < 34" could be an operand will depend on
one's background. E.g., if you had been exposed to the Icon
programming language, you might expect that conditionals can
produce non-Boolean values, as will comparisons (which can be chained…).
So your brain will need to harbour yet another significant set of rules
if work life forces you to work with yet another language.
Time well spent? The combinatorial complexity is foreseeable,
as is the opportunity to specialize. If employers pay for it, good,
enjoy.

The real trouble is, I think, to allow many peculiarities into
the syntax for what is essentially the same thing, conditional
execution. In this case one has to decipher the expression
inside out, first, and know the rules. This works as long as one
is accustomed to just that syntax. It will work better if there are
indicators that let one expect modifiers, I think, as is the
case for Python's built-in filtering ifs.

I'm not that happy adding parentheses around \$a += \$b because
have an effect like establishing list context. And then, whether
list context makes a difference. But, in Perl there is no real way
to do it if one wishes to invert the control flow grammatically.
There is some stretch of context and implicit grammar, and both
are required of every reader for that convenience of a one-liner.

The density adds a flavor of APL, though ;-)

## Re: prime

No. It depends on the programming language in question and while it is
certainly possible to use if as binary operator and define some
semantics which cause an expression

\$b if \$c < 34

to evaluate to some value  if \$c is not smaller then 34, eg, 42 (for
hopefully obvious reasons) or special-case this in the parser to mean
circumference / diamater * times average income tax rate in 1977 (any
information which could serve as a hint regarding 'circumeference and
diameter of what' or 'income tax paid where' intentionally omitted to
stress the fact that words are meaningless and communication futile), this
is not the case in Perl.

I don't memoize things, I acquire them as the need arises and gradually
lose them again (sort of) when it goes away. The idea that a human
brain is best used as container for vast amounts of information which is
useless almost all of the time while intelligent computers are needed to
provide the intelligence this arrangement otherwise lacks is - well
- unintelligent (probably came from a guy who never drove a car down a
long slope at maximum acceleration to determine just how fast it can go).

But I realize this doesn't really answer the question.

Trying to read it from left to right is sufficient, although possibly
not consistent with 'the rules' as dictated (and dutifully internalized)
by some other programming language. But I don't really understand
this. A construct like

if (\$c < 34) {
print("Remember the income tax\n");
}

can be said to have an 'inside position' (between the brackets) and also
something which is outside of that but not

print("Remember the income tax\n") if \$c < 34;

except as automatic superimposition of a mind expecting to see the
former (Wild guess. I really don't understand it).

[...]

It's sort of silly to create 'groups of one element', especially if this
means adding yet more strange characters (like {) to a text: The curlies
(or any similar construct) are a sort-of a makeshift approach for
telling the computer that sequence of statements should be controlled by
the if (or a similar construct) instead of just the one it is attached
to. And "ok, that's good enough to express anything and nobody pays me
was criticizing: The more primitive the used language happens to be, the
more complicated does it become to describe a complex circumstance in it
and the more complicated the description, the more cumbersome is it to
understand what it means.

## Re: prime

No.

statement unless condition;

is equivalent to

unless (condition) {
statement
}

(except for scope).

So

\$N=0, last unless \$N % \$_;

is equivalent to

unless (\$N % \$_) {
\$N=0, last;
}

Just written differently, just like you can switch the order in many
natural languages.

hp

--
_  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel

## Re: prime

On 20/10/2014 10:50 μμ, John Black wrote:

ok you are right its an ugly (but very fast) hack.
The normal (slower) equivelant is

if ( 0 == \$N % \$_ )
{
\$N=0;
last
}

## Re: prime

George, thanks.  You are right that this code is noticably slower.  But I experimented and
found that

if (!(\$N % \$_)) {
\$N = 0; last;
}

performs just as well as the comma version.  Somehow the comparision to exactly 0 slows it
way down though...

John Black

## Re: prime

manner?

Yes.

because \$N is the number currently being examined and if it is divisable
by \$_ (i.e. any other integer) it is not a prime. So \$N is set to zero
(known not to be prime and tested for after the loop) and the loop is
exited. If the the loop reaches the end without \$N being set to 0, then
\$N is a prime.

hp

--
_  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel

## Re: prime

[...]

I have to admit that I read this with interest but this is really so
bleeding obvious that it shouldn't need this many words. Let r be the
square root of x then

r * r = x

hence, for every two numbers s >= r, t > r: s * t > x. If no factors
smaller than r were found, there can't be any factors larger than r.