mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2012-10-25, 02:10   #1
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

10000000100112 Posts
Default pari break() usage

pari gurus,

Can you tell me why I can't seem to get the break command to break within a loop, instead of executing the rest of the loop prior to breaking?
Code:
findcycle(a,b)=
{
    for(e=a,b,
    i=e;
    for(c=1,10,
      i=sigma(i)-i;
      if(i<e,
        break());
      if(i==e,
        print(i,"  ",c);
        break()));
      print(e))
}
I want the ifs to break out of the inner loop immediately, but they don't; they continue to the end of the loop invoking the print(e). That means if the first if is true, the second uselessly gets evaluated anyway as well as the print(e) line. If I use break(2), it breaks the outside for loop.

BTW, I have tried next(), with identical results.

I guess along an alternate route, is there an else type command?

Thanks!
EdH is offline   Reply With Quote
Old 2012-10-25, 02:18   #2
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

32×1,069 Posts
Default

Quote:
Originally Posted by EdH View Post
I want the ifs to break out of the inner loop immediately, but they don't; they continue to the end of the loop invoking the print(e).
No, they do. The inner loop is indeed broken when you ordered it _and then_ print(e) is executed. Because it is outside of the inner loop.
Batalov is offline   Reply With Quote
Old 2012-10-25, 02:21   #3
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

32×1,069 Posts
Default

If you indent your own code correctly, you will see it easier:
Code:
findcycle(a,b)=
{
  for(e=a,b,
    i=e;
    for(c=1,10,
      i=sigma(i)-i;
      if(i<e,
        break());
      if(i==e,
        print(i,"  ",c);
        break())
    );
    print(e)
  )
}
Batalov is offline   Reply With Quote
Old 2012-10-25, 02:52   #4
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

411510 Posts
Default

Quote:
Originally Posted by Batalov View Post
If you indent your own code correctly, you will see it easier:
Thanks! I was chopping down a much more involved version and didn't do a good job. That will teach me - probably not.

(I'm glad I used my actual code, instead of "constructing" something for the post...)

Again, thanks!
EdH is offline   Reply With Quote
Old 2012-10-25, 03:04   #5
Batalov
 
Batalov's Avatar
 
"Serge"
Mar 2008
Phi(4,2^7658614+1)/2

32·1,069 Posts
Default

Oh, I do it myself all the time. When I prototype PE problems in Pari, I frequently type a monstrous ever-growing one-liner (without indentation, obviously). It's a pain to watch later. Here's an example:
Code:
forprime(p=2,10000,if(p%10==1||p%10==9,d=divisors(p-1);for(j=2,#d,i=d[j]*p;if(i>10^8,break);if(fibonacci(i)%p^2==0,write("presieve1",p" "i);break))))
(I rewrote it in C, later; Not only rewrote much prettier but without even touching fibonacci(); this was just brainstorming -- Uggggh!)
Batalov is offline   Reply With Quote
Old 2012-10-25, 03:40   #6
EdH
 
EdH's Avatar
 
"Ed Hall"
Dec 2009
Adirondack Mtns

5×823 Posts
Default

Quote:
Originally Posted by Batalov View Post
Oh, I do it myself all the time. When I prototype PE problems in Pari, I frequently type a monstrous ever-growing one-liner (without indentation, obviously). It's a pain to watch later. Here's an example:
Code:
forprime(p=2,10000,if(p%10==1||p%10==9,d=divisors(p-1);for(j=2,#d,i=d[j]*p;if(i>10^8,break);if(fibonacci(i)%p^2==0,write("presieve1",p" "i);break))))
(I rewrote it in C, later; Not only rewrote much prettier but without even touching fibonacci(); this was just brainstorming -- Uggggh!)
I used to write stuff like that in a single cell in Excel - I kept bumping up against the character limit. I still have a scoring workbook in use in several places across the country that won't work with greater than 999 competitors due to the cell limitation (unless newer versions expanded that limit). I've been trying to clean my code up lately, what code I still write. Someday, I have to actually comment things. I've run into the problem of not being able to read my own code for lack of comments.

Hmm... I'm rambling... Gotta go.

Thanks again.
EdH is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
LLL in GP/Pari paul0 Programming 2 2015-11-17 13:04
Break up songs davieddy Lounge 4 2013-03-17 01:26
Did that guy actually break the the sound barrier? davieddy Puzzles 47 2012-10-19 21:12
What do I have to do to break free? fivemack Aliquot Sequences 8 2010-04-08 18:47
64-bit Pari? CRGreathouse Software 2 2009-03-13 04:22

All times are UTC. The time now is 00:35.


Tue Dec 7 00:35:38 UTC 2021 up 136 days, 19:04, 1 user, load averages: 0.77, 0.87, 0.98

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.