Python 3.3 vs. MSDOS Basic

Alexander Blinne news at blinne.net
Tue Feb 19 19:23:27 EST 2013


Am 19.02.2013 12:42, schrieb Piet van Oostrum:
> Terry Reedy <tjreedy at udel.edu> writes:
>> I find this surprising too. I am also surprised that it even works,
>> given that the highest intermediate value is about 57 billion and I do
>> not remember that Basic had infinite precision ints.
> 
> That may explain why the Basic version is faster: it gets overflow and
> then it may have taken some shortcuts.

Consider this C program

#include <stdio.h>

int main(void) {

  int max = 0;
  int m = 0;
  long int n;
  int count;
  int num;

  while(m<=1000000) {
    m++;
    n = m;
    count = 0;

    while(n != 1) {
      count++;
      if(n % 2 == 0) {
        n = n / 2;
      }
      else {
        n = n*3 + 1;
      }
    }

    if(count > max) {
      max = count;
      num = m;
    }
  }

  printf("%d, %d\n", num, max);
}

If the line

long int n;

is changed into

unsigned int n;

the program runs in 0.68 sec instead of 0.79, so there is some shortcut.
If changed into

signed int n;

there is a veeery long, perhaps infinite loop.

Greetings
Alexander




More information about the Python-list mailing list