Den onsdagen den 16:e juli 2014 kl. 16:58:29 UTC+2 skrev jonas.t...@gmail.com:
> Den onsdagen den 16:e juli 2014 kl. 16:23:41 UTC+2 skrev Scott Sauyet:
>
> > jonas.thornvall@gmail.com wrote:
>
> >
>
> > > number=29;
>
> >
>
> > > basemultip=1;
>
> >
>
> > > base=3;
>
> >
>
> > >
>
> >
>
> > > while (basemultip < number)
>
> >
>
> > > {
>
> >
>
> > > prevmultip=basemultip;
>
> >
>
> > > basemultip = basemultip * base;
>
> >
>
> > >
>
> >
>
> > > }
>
> >
>
> > > basemultip=prevbase;
>
> >
>
> > >
>
> >
>
> > > What is the solution to have the previous value when you walk
>
> >
>
> > > out from loop, i think my code look strange and i probably
>
> >
>
> > > learned how to do this but forgot about it.
>
> >
>
> > >
>
> >
>
> > > Can i do it without buffering the previous value?
>
> >
>
> >
>
> >
>
> > Doing this in a function might help. One approach:
>
> >
>
> >
>
> >
>
> > var findLast = function(base, number) {
>
> >
>
> > var current = 1;
>
> >
>
> > while (true) {
>
> >
>
> > next = base * current;
>
> >
>
> > if (next >= number) {return current;}
>
> >
>
> > current = next;
>
> >
>
> > }
>
> >
>
> > }
>
> >
>
> >
>
> >
>
> > findLast(3, 29); //=> 27
>
> >
>
> >
>
> >
>
> > Note that this has no error checking. You might want to check that
>
> >
>
> > your target is greater than 1, and that your base is greater than 1.
>
> >
>
> >
>
> >
>
> > But note that this could easily be abstracted into a more general
>
> >
>
> > pattern, which might itself be useful:
>
> >
>
> >
>
> >
>
> > var findLast = function(start, getNext, predicate) {
>
> >
>
> > var current = start;
>
> >
>
> > while (true) {
>
> >
>
> > next = getNext(current);
>
> >
>
> > if (!predicate(next)) {return current;}
>
> >
>
> > current = next;
>
> >
>
> > }
>
> >
>
> > }
>
> >
>
> >
>
> >
>
> > var base = 3, number = 29;
>
> >
>
> > findLast(1, function(n) {return base * n;},
>
> >
>
> > function(n) {return n < number;}); //=> 27
>
> >
>
> >
>
> >
>
> > Again this is missing some error checking. You might want to at
>
> >
>
> > least test that the initial value passes the predicate:
>
> >
>
> >
>
> >
>
> > if (!predicate(start)) {throw new Error('Illegal initial value');}
>
> >
>
> >
>
> >
>
> > It would be harder in this general case though to ensure that we'll ever hit the termination condition.
>
> >
>
> >
>
> >
>
> > -- Scott
>
>
>
> It seem to use two conditional checks, While(true) and if(predicate) but maybe it still faster by not buffering the value each loop?
>
>
>
> I think it kind of weird that there seem to be no loop construction that could catch this case.
I tried to get different timings, but i do something wrong evidently.
<SCRIPT language=Javascript>
basemultip=1;
prevmultip=1;
var prevone = function(base,number){
while (basemultip < number)
{
prevmultip=basemultip;
basemultip = basemultip * base;
}
return prevmultip;
}
var prevtwo = function(base, number) {
var current = 1;
while (true) {
next = base * current;
if (next >= number) {return current;}
current = next;
}
}
var prevthree = function(base,number){
var basemultip, prevmultip = 1;
while ((basemultip = prevmultip * base) < number) prevmultip = basemultip;
return basemultip
}
var start = new Date().getTime();
for(i=1;i<1000;i++){ prevone(7,i); document.write(prevmultip," ");}
//for(i=1;i<1000;i++){ prevtwo(7,i); document.write(basemultip," ");}
//for(i=1;i<1000;i++){ prevthree(7,i); document.write(prevmultip," ");}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);
</script>