Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101820 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2016-01-16 14:23 -0800 |
| Last post | 2016-01-17 22:32 +1100 |
| Articles | 10 — 6 participants |
Back to article view | Back to comp.lang.python
Keen eyes jonas.thornvall@gmail.com - 2016-01-16 14:23 -0800
Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 09:30 +1100
Re: Keen eyes jonas.thornvall@gmail.com - 2016-01-16 14:55 -0800
Re: Keen eyes jonas.thornvall@gmail.com - 2016-01-16 15:25 -0800
Re: Keen eyes Steven D'Aprano <steve@pearwood.info> - 2016-01-17 18:10 +1100
Re: Keen eyes Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-17 00:37 -0700
Re: Keen eyes Paul Rubin <no.email@nospam.invalid> - 2016-01-16 23:52 -0800
Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 19:25 +1100
Re: Keen eyes BartC <bc@freeuk.com> - 2016-01-17 11:17 +0000
Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 22:32 +1100
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-01-16 14:23 -0800 |
| Subject | Keen eyes |
| Message-ID | <d8e5eaf4-c59f-4635-8e73-f4a9a1a84877@googlegroups.com> |
This is not python just a short snippet of javascript that refuse tracing, i've staired blind upon it but since it does something weird with allocating memory i have no idea what is going on and the parrots and monkeys at comp.lang.javascript refuse to give a hint.
Something in those loops really wrong but it is no giant numbers.
<script language="Javascript">
function factor_it(i){
prime=true;
sqroot=Math.floor(Math.sqrt(i));
for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
return prime;
}
function main(){
base=10;
outStr="";
//while (base==10) {
j=1;
while(j<base){
document.prime.trash.value =" J ="+j+",";
prime=false;
i=j;
i=i+base;
while (i<100){
primeleg=factor_it(i);
if(primeleg==true){break;}
i=i+base;
}
outStr+="Base="+base+" counterval i = "+i+" P primeleg ="+primeleg+"\n";
j++;
}
//base++;
//}
document.prime.out.value =outStr;
}
</script>
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body onload="main()" bgcolor="gold">
<form name="prime" action="" onsubmit="MAIN(); return false;">
<textarea name="out" cols=80 rows=80></textarea>
<textarea name="trash" cols=80 rows=80></textarea>
</form>
</body>
</html>
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-17 09:30 +1100 |
| Message-ID | <mailman.47.1452983435.15297.python-list@python.org> |
| In reply to | #101820 |
On Sun, Jan 17, 2016 at 9:23 AM, <jonas.thornvall@gmail.com> wrote:
> function factor_it(i){
> prime=true;
> sqroot=Math.floor(Math.sqrt(i));
> for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
> return prime;
> }
A couple of potential problems here. The first thing that comes to
mind is that floating point inaccuracy is going to bite you long
before the numbers "seem huge" to someone who's thinking about 2**53.
The second is an off-by-one error: a perfect square may come up as
prime.
Check for those and see how it looks.
Also, check your double-use of the 'prime' variable, which also
appears to be global here.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-01-16 14:55 -0800 |
| Message-ID | <2686546a-b127-4250-a553-3f2e4fb70633@googlegroups.com> |
| In reply to | #101821 |
Den lördag 16 januari 2016 kl. 23:30:48 UTC+1 skrev Chris Angelico:
> On Sun, Jan 17, 2016 at 9:23 AM, <jonas.thornvall@gmail.com> wrote:
> > function factor_it(i){
> > prime=true;
> > sqroot=Math.floor(Math.sqrt(i));
> > for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
> > return prime;
> > }
>
> A couple of potential problems here. The first thing that comes to
> mind is that floating point inaccuracy is going to bite you long
> before the numbers "seem huge" to someone who's thinking about 2**53.
> The second is an off-by-one error: a perfect square may come up as
> prime.
>
> Check for those and see how it looks.
>
> Also, check your double-use of the 'prime' variable, which also
> appears to be global here.
>
> ChrisA
Thank you Chris, this is not really for factoring it is meant to create a composite sieve. Well all the legs just holding composites will be false.
Regarding the problem no number bigger than 100 is sent to the function, there is something weird with either the loop structure or the break.
What should happen...
j=1
i=j
1+10 break (because prime in leg)
j++; well j is 2 and so is i
2+10,2+20,2+30,2+40.2+50,2+60,2+70,2+80,2+90 ((condidion break loop i>100
j++ ->i=3
3+10 break (because prime in leg)
j++
And so on...
And as you can see the outer loop does this until j reaches ***base which is 10***.
In all it is about 50 operations
So how can it allocate all memory?
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2016-01-16 15:25 -0800 |
| Message-ID | <7625bba2-5c2d-4d28-8b6a-30ec7b490089@googlegroups.com> |
| In reply to | #101821 |
Den lördag 16 januari 2016 kl. 23:30:48 UTC+1 skrev Chris Angelico:
> On Sun, Jan 17, 2016 at 9:23 AM, <jonas.thornvall@gmail.com> wrote:
> > function factor_it(i){
> > prime=true;
> > sqroot=Math.floor(Math.sqrt(i));
> > for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
> > return prime;
> > }
>
> A couple of potential problems here. The first thing that comes to
> mind is that floating point inaccuracy is going to bite you long
> before the numbers "seem huge" to someone who's thinking about 2**53.
> The second is an off-by-one error: a perfect square may come up as
> prime.
>
> Check for those and see how it looks.
>
> Also, check your double-use of the 'prime' variable, which also
> appears to be global here.
>
> ChrisA
Thank you Chris your comment resolved it double use of j in two different functions and loops.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-01-17 18:10 +1100 |
| Message-ID | <569b3e53$0$1615$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #101826 |
On Sun, 17 Jan 2016 10:25 am, jonas.thornvall@gmail.com wrote:
> double use of j in two different functions
Are you using a global variable called "j" as a loop variable? That sounds
like a terrible idea.
You should use local variables. Then a function with a local variable j
cannot possibly effect another function with a local variable also called
j.
Wait... is somebody going to tell me that Javascript defaults to global
variables inside functions?
js> function a(){
> for (j=2;j<10;j++){}
> return 1
> }
js> function b(){
> for (j=2; j<20;j++){}
> return 1
> }
js> j
js: "<stdin>", line 13: uncaught JavaScript runtime exception:
ReferenceError: "j" is not defined.
at <stdin>:13
js> a()
1
js> j
10
js> b()
1
js> j
20
And this is the language that 95% of the Internet uses... my brain hurts.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-01-17 00:37 -0700 |
| Message-ID | <mailman.55.1453016223.15297.python-list@python.org> |
| In reply to | #101833 |
On Jan 17, 2016 12:16 AM, "Steven D'Aprano" <steve@pearwood.info> wrote: > > On Sun, 17 Jan 2016 10:25 am, jonas.thornvall@gmail.com wrote: > > > double use of j in two different functions > > Are you using a global variable called "j" as a loop variable? That sounds > like a terrible idea. > > You should use local variables. Then a function with a local variable j > cannot possibly effect another function with a local variable also called > j. > > Wait... is somebody going to tell me that Javascript defaults to global > variables inside functions? Technically it defaults to non local. The var statement allocates a variable within the current scope. Otherwise it searches up the chain of parent scopes for a matching variable, terminating at the global scope. I believe Lua also works this way.
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2016-01-16 23:52 -0800 |
| Message-ID | <871t9gy762.fsf@nightsong.com> |
| In reply to | #101833 |
Steven D'Aprano <steve@pearwood.info> writes: > And this is the language that 95% of the Internet uses... my brain hurts. WAT. https://www.youtube.com/watch?v=20BySC_6HyY
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-17 19:25 +1100 |
| Message-ID | <mailman.56.1453019131.15297.python-list@python.org> |
| In reply to | #101833 |
On Sun, Jan 17, 2016 at 6:37 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: > Technically it defaults to non local. The var statement allocates a > variable within the current scope. Otherwise it searches up the chain of > parent scopes for a matching variable... This far, it's exactly the same as C. > ... terminating at the global scope. This is the bit that gets ridiculous. Undeclared variables in C are compile-time errors. Undeclared variables in JS are implicit globals. This is stupid. Oh, plus the fact that you can have "var x;" anywhere but it always applies to the whole function. But most of the time, that won't bite you. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-01-17 11:17 +0000 |
| Message-ID | <n7ft2d$f7b$1@dont-email.me> |
| In reply to | #101836 |
On 17/01/2016 08:25, Chris Angelico wrote: > On Sun, Jan 17, 2016 at 6:37 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: >> Technically it defaults to non local. The var statement allocates a >> variable within the current scope. Otherwise it searches up the chain of >> parent scopes for a matching variable... > > This far, it's exactly the same as C. > >> ... terminating at the global scope. > > This is the bit that gets ridiculous. Undeclared variables in C are > compile-time errors. Undeclared variables in JS are implicit globals. > This is stupid. My own language is something in-between. If a name is not declared locally, it will look at more global scopes. If nothing is found, it will auto-declare a local. So the JS bug wouldn't occur. However, there is the problem that, given a perfectly working function with implicitly locals, at some point in the future someone could introduce a global that will clash with the name of a local, and screw things up. Because of that, the Python scheme is better on the whole. The only issue is that sometimes you think you're assigning to a global, but it's really a local if you forget the 'global' declaration within the function. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2016-01-17 22:32 +1100 |
| Message-ID | <mailman.60.1453030378.15297.python-list@python.org> |
| In reply to | #101845 |
On Sun, Jan 17, 2016 at 10:17 PM, BartC <bc@freeuk.com> wrote: > On 17/01/2016 08:25, Chris Angelico wrote: >> >> On Sun, Jan 17, 2016 at 6:37 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: >>> >>> Technically it defaults to non local. The var statement allocates a >>> variable within the current scope. Otherwise it searches up the chain of >>> parent scopes for a matching variable... >> >> >> This far, it's exactly the same as C. >> >>> ... terminating at the global scope. >> >> >> This is the bit that gets ridiculous. Undeclared variables in C are >> compile-time errors. Undeclared variables in JS are implicit globals. >> This is stupid. > > > My own language is something in-between. If a name is not declared locally, > it will look at more global scopes. If nothing is found, it will > auto-declare a local. > > So the JS bug wouldn't occur. However, there is the problem that, given a > perfectly working function with implicitly locals, at some point in the > future someone could introduce a global that will clash with the name of a > local, and screw things up. > > Because of that, the Python scheme is better on the whole. The only issue is > that sometimes you think you're assigning to a global, but it's really a > local if you forget the 'global' declaration within the function. The Python scheme makes a lot of logical sense. The C scheme (declare everything, otherwise it's an error) makes a lot of logical sense. I've never seen a hybrid scheme that makes as much sense as either of the above. In both Python's and C's ways of doing things, you can look at a single function and know which names are local and which are external. Have fun trying to share code snippets without knowing full context... ChrisA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web