Click here to Skip to main content
Click here to Skip to main content

ObjectScript: A new programming language

By , 9 Oct 2012
 
OS-0.98-rc-noexe.ZIP
OS
examples-os
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
proj.win32
add_user_module
add_user_module.vcxproj.filters
os
os.vcxproj.filters
osbind
osbind.vcxproj.filters
profile_benchmark
profile_benchmark.vcxproj.filters
scripts
test_fannkuch.lua
test_fannkuch.os
run_os_prog
run_os_prog.vcxproj.filters
stack_usage
stack_usage.vcxproj.filters
README.md
source
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
OS-0.98-rc.ZIP
bin
os.exe
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
Makefile
add_user_module.vcxproj.filters
os.vcxproj.filters
osbind.vcxproj.filters
profile_benchmark.vcxproj.filters
test_fannkuch.lua
test_fannkuch.os
run_os_prog.vcxproj.filters
stack_usage.vcxproj.filters
README.md
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
unitpoint-objectscript-86f30d7-noexe.zip
unitpoint-objectscript-86f30d7
.gitignore
bin
.gitignore
examples-os
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
proj.win32
.gitignore
add_user_module
.gitignore
os
.gitignore
test_fannkuch.osc
test_fannkuch.osd
osbind
.gitignore
profile_benchmark
.gitignore
scripts
test_fannkuch.lua
test_fannkuch.os
run_os_prog
.gitignore
stack_usage
.gitignore
README.md
source
.gitignore
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
unitpoint-objectscript-86f30d7.zip
.gitignore
.gitignore
os.exe
add_user_module.os
bind.os
core.os
run_os_prog.os
test.os
Makefile
.gitignore
.gitignore
.gitignore
test_fannkuch.osc
test_fannkuch.osd
.gitignore
.gitignore
test_fannkuch.lua
test_fannkuch.os
.gitignore
.gitignore
README.md
.gitignore
os-binder-FunctionClassImp.tpl
os-binder-FunctionImp.tpl
os-binder-FunctionImpVoid.tpl
<?php

function fannkuch($n)
{
  $p = array();
  $q = array();
  $s = array();
  $sign = 1;
  $maxflips = $sum = 0;
  for($i=1; $i<=$n; $i++) $p[$i] = $q[$i] = $s[$i] = $i;
  for(;;){
    // Copy and flip.
    $q1 = $p[1];				// Cache 1st element.
    if($q1 != 1){
      for($i=2; $i<=$n; $i++) $q[$i] = $p[$i];		// Work on a copy.
      $flips = 1;
      for(;;){
		$qq = $q[$q1];
		if($qq == 1){				// ... until 1st element is 1.
		  $sum += $sign*$flips;
		  if($flips > $maxflips){
			$maxflips = $flips;
		  } // New maximum?
		  break;
		}
		$q[$q1] = $q1;
		if($q1 >= 4){
		  $i = 2; $j = $q1 - 1;
		  for(;;){ $tmp = $q[$i]; $q[$i] = $q[$j]; $q[$j] = $tmp; if(++$i >= --$j) break; }
		}
		$q1 = $qq; $flips++;
      }
    }
    // Permute.
    if($sign == 1){
      $tmp = $p[2]; $p[2] = $p[1]; $p[1] = $tmp; $sign = -1;	// Rotate 1<-2.
    }else{
      $tmp = $p[2]; $p[2] = $p[3]; $p[3] = $tmp; $sign = 1;	// Rotate 1<-2 and 1<-2<-3.
      for($i = 3;; $i++){
		$sx = $s[$i];
		if($sx != 1){ $s[$i] = $sx-1; break; }
		if($i == $n) return array($sum, $maxflips);	// Out of permutations.
		$s[$i] = $i;
		// Rotate 1<-...<-i+1.
		$t = $p[1]; for($j = 1; $j <= $i; $j++){ $p[$j] = $p[$j+1]; } $p[$i+1] = $t;
      }
    }
  }
}

function getTimeSec(){
    list($usec, $sec) = explode(" ",microtime());
    return ($usec + $sec);
}

$n = isset($argv[1]) ? $argv[1] : 5;
echo "n: $n\n";
$start_time = getTimeSec();
$r = fannkuch($n);
$sum = $r[0]; $flips = $r[1];
echo("$sum\nPfannkuchen($n) = $flips\n"
	. "time = ".(getTimeSec() - $start_time)."\n");

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

unitpoint
United States United States
Member
No Biography provided

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 9 Oct 2012
Article Copyright 2012 by unitpoint
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid