Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Maintain the same structure and functionality when rewriting this code in Factor.
package main import ( "fmt" "math" ) type float float64 func (f float) p(e float) float { return float(math.Pow(float64(f), float64(e))) } func main() { ops := []string{"-x.p(e)", "-(x).p(e)", "(-x).p(e)", "-(x.p(e))"} for _, x := range []float{float(-5), float(5)} { for _, e := range []floa...
USING: infix locals prettyprint sequences sequences.generalizations sequences.repeating ; :: row ( x p -- seq ) x p "-x**p" [infix -x**p infix] "-(x)**p" [infix -(x)**p infix] "(-x)**p" [infix (-x)**p infix] "-(x**p)" [infix -(x**p) infix] 10 narray ; { "x value" "p value" } { "expression" "result" } ...
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "fmt" "rcu" "sort" ) func main() { const LIMIT = 99 primes := rcu.Primes(LIMIT) rmap := make(map[int]bool) for _, p := range primes { for _, q := range primes { var pq int if q < 10 { pq = p*10 + q } else { ...
USING: formatting grouping io kernel math.parser math.primes present prettyprint sequences sets sorting ; "Concatenated-pair primes from primes < 100:" print nl 99 primes-upto [ present ] map dup [ append dec> ] cartesian-map concat [ prime? ] filter members natural-sort [ length ] keep 8 group simple-table. "\nFound ...
Change the following Go code into Factor without altering its purpose.
package main import ( "fmt" "rcu" "sort" ) func main() { const LIMIT = 99 primes := rcu.Primes(LIMIT) rmap := make(map[int]bool) for _, p := range primes { for _, q := range primes { var pq int if q < 10 { pq = p*10 + q } else { ...
USING: formatting grouping io kernel math.parser math.primes present prettyprint sequences sets sorting ; "Concatenated-pair primes from primes < 100:" print nl 99 primes-upto [ present ] map dup [ append dec> ] cartesian-map concat [ prime? ] filter members natural-sort [ length ] keep 8 group simple-table. "\nFound ...
Port the following code from Go to Factor with equivalent syntax and logic.
package main import ( "fmt" "strings" ) type test struct { bs string n int } func setRightBits(bits []byte, e, n int) []byte { if e == 0 || n <= 0 { return bits } bits2 := make([]byte, len(bits)) copy(bits2, bits) for i := 0; i < e-1; i++ { c := bits[i] if...
USING: formatting io kernel math math.parser math.ranges sequences ; : set-rab ( n b -- result ) [0,b] [ neg shift ] with [ bitor ] map-reduce ; :: show ( n b e -- ) b e "n = %d; width = %d\n" printf n n b set-rab [ >bin e CHAR: 0 pad-head print ] bi@ ; { 0b1000 0b0100 0b0010 0b0000 } [ 2 4 show nl ] eac...
Translate this program into Factor but keep the logic exactly as in Go.
package main import ( "fmt" "sort" "sync" "time" ) type history struct { timestamp tsFunc hs []hset } type tsFunc func() time.Time type hset struct { int t time.Time } func newHistory(ts tsFunc) history { return history{ts, []hset{{t: ts()}}} } ...
USING: accessors combinators formatting kernel models.history ; 1 <history> { [ add-history ] [ value>> "Initial value: %u\n" printf ] [ 2 >>value add-history ] [ 3 swap value<< ] [ value>> "Current value: %u\n" printf ] [ go-back ] [ go-back ] [ value>> "Restored value: %u\n" printf ] ...
Convert this Go snippet to Factor and keep its semantics consistent.
package main import ( "fmt" "math/rand" "regexp" "time" ) const base = "ACGT" func findDnaSubsequence(dnaSize, chunkSize int) { dnaSeq := make([]byte, dnaSize) for i := 0; i < dnaSize; i++ { dnaSeq[i] = base[rand.Intn(4)] } dnaStr := string(dnaSeq) dnaSubseq := make([]byte...
USING: accessors formatting grouping io kernel math math.functions.integer-logs math.parser random regexp sequences ; : new-dna ( n -- str ) [ "ACGT" random ] "" replicate-as ; : pad ( n d -- str ) [ number>string ] dip 32 pad-head ; :: .dna ( seq n -- ) seq length integer-log10 1 + :> d seq n group [ n * d ...
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "bytes" "fmt" "io/ioutil" "log" "runtime" ) func main() { fileName1 := "rodgers.txt" fileName2 := "rodgers_reversed.txt" lineBreak := "\n" if runtime.GOOS == "windows" { lineBreak = "\r\n" } b, err := ioutil.ReadFile(fileName1) if err ...
USING: io io.encodings.utf8 io.files sequences ; "rodgers.txt" utf8 file-lines <reversed> [ print ] each
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "fmt" "rcu" ) func main() { fmt.Println("Decimal numbers under 1,000 whose digits include two 1's:") var results []int for i := 11; i <= 911; i++ { digits := rcu.Digits(i, 10) count := 0 for _, d := range digits { if d == 1 { ...
USING: io math math.functions prettyprint sequences sequences.extras ; { 0 2 3 4 5 6 7 8 9 } [| n | { { n 1 1 } { 1 n 1 } { 1 1 n } } ] map-concat [ <reversed> 0 [ 10^ * + ] reduce-index pprint bl ] each nl
Ensure the translated Factor code behaves exactly like the original Go snippet.
package main import ( "fmt" "sort" ) func main() { strings := []string{"1a3c52debeffd", "2b6178c97a938stf", "3ycxdb1fgxa2yz"} u := make(map[rune]int) for _, s := range strings { m := make(map[rune]int) for _, c := range s { m[c]++ } for k, v := range m {...
USING: io kernel sequences.interleaved sets sorting ; { "1a3c52debeffd" "2b6178c97a938sf" "3ycxdb1fgxa2yz" } [ intersect-all ] [ [ duplicates ] gather without ] bi natural-sort CHAR: space <interleaved> print
Convert this Go snippet to Factor and keep its semantics consistent.
package main import ( "fmt" "math/big" ) func jacobsthal(n uint) *big.Int { t := big.NewInt(1) t.Lsh(t, n) s := big.NewInt(1) if n%2 != 0 { s.Neg(s) } t.Sub(t, s) return t.Div(t, big.NewInt(3)) } func jacobsthalLucas(n uint) *big.Int { t := big.NewInt(1) t.Lsh(t, n...
USING: grouping io kernel lists lists.lazy math math.functions math.primes prettyprint sequences ; : 2^-1^ ( n -- 2^n -1^n ) dup 2^ -1 rot ^ ; : jacobsthal ( m -- n ) 2^-1^ - 3 / ; : jacobsthal-lucas ( m -- n ) 2^-1^ + ; : as-list ( quot -- list ) 0 lfrom swap lmap-lazy ; inline : jacobsthals ( -- list ) [ jacobsthal ...
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" "math/big" ) func jacobsthal(n uint) *big.Int { t := big.NewInt(1) t.Lsh(t, n) s := big.NewInt(1) if n%2 != 0 { s.Neg(s) } t.Sub(t, s) return t.Div(t, big.NewInt(3)) } func jacobsthalLucas(n uint) *big.Int { t := big.NewInt(1) t.Lsh(t, n...
USING: grouping io kernel lists lists.lazy math math.functions math.primes prettyprint sequences ; : 2^-1^ ( n -- 2^n -1^n ) dup 2^ -1 rot ^ ; : jacobsthal ( m -- n ) 2^-1^ - 3 / ; : jacobsthal-lucas ( m -- n ) 2^-1^ + ; : as-list ( quot -- list ) 0 lfrom swap lmap-lazy ; inline : jacobsthals ( -- list ) [ jacobsthal ...
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import "fmt" func sieve(limit int) []int { primes := []int{2} c := make([]bool, limit+1) p := 3 for { p2 := p * p if p2 > limit { break } for i := p2; i <= limit; i += 2 * p { c[i] = true } for { p +...
USING: formatting fry grouping kernel math math.primes math.statistics sequences ; IN: rosetta-code.successive-prime-differences : seq-diff ( seq diffs -- seq' quot ) dup [ length 1 + <clumps> ] dip '[ differences _ sequence= ] ; inline : show ( seq diffs -- ) [ "...for differences %u:\n" printf ] keep se...
Generate an equivalent Factor version of this Go code.
package main import ( "fmt" "rcu" ) func main() { var numbers []int for i := 2; i < 200; i++ { bds := rcu.DigitSum(i, 2) if rcu.IsPrime(bds) { tds := rcu.DigitSum(i, 3) if rcu.IsPrime(tds) { numbers = append(numbers, i) } } ...
USING: combinators combinators.short-circuit formatting io lists lists.lazy math math.parser math.primes sequences ; : dsum ( n base -- sum ) >base [ digit> ] map-sum ; : dprime? ( n base -- ? ) dsum prime? ; : 23prime? ( n -- ? ) { [ 2 dprime? ] [ 3 dprime? ] } 1&& ; : l23primes ( -- list ) 1 lfrom [ 23prime? ] lfilt...
Generate an equivalent Factor version of this Go code.
package main import "fmt" func isPrime(n int) bool { if n == 1 { return false } i := 2 for i*i <= n { if n%i == 0 { return false } i++ } return true } func main() { var final, pNum int for i := 1; pNum < 10001; i++ { if isPrime(i) {...
USING: math math.primes prettyprint ; 2 10,000 [ next-prime ] times .
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import "fmt" func isPrime(n int) bool { if n == 1 { return false } i := 2 for i*i <= n { if n%i == 0 { return false } i++ } return true } func main() { var final, pNum int for i := 1; pNum < 10001; i++ { if isPrime(i) {...
USING: math math.primes prettyprint ; 2 10,000 [ next-prime ] times .
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(999) sum, n, c := 0, 0, 0 fmt.Println("Summing the first n primes (<1,000) where the sum is itself prime:") fmt.Println(" n cumulative sum") for _, p := range primes { n++ sum += p if rcu.IsPri...
USING: assocs formatting kernel math.primes math.ranges math.statistics prettyprint ; 1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip [ nip prime? ] assoc-filter [ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(999) sum, n, c := 0, 0, 0 fmt.Println("Summing the first n primes (<1,000) where the sum is itself prime:") fmt.Println(" n cumulative sum") for _, p := range primes { n++ sum += p if rcu.IsPri...
USING: assocs formatting kernel math.primes math.ranges math.statistics prettyprint ; 1000 [ [1,b] ] [ primes-upto cum-sum ] bi zip [ nip prime? ] assoc-filter [ "The sum of the first %3d primes is %5d (which is prime).\n" printf ] assoc-each
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import ( "fmt" "strconv" "strings" ) func divByAll(num int, digits []byte) bool { for _, digit := range digits { if num%int(digit-'0') != 0 { return false } } return true } func main() { magic := 9 * 8 * 7 high := 9876432 / magic * magic fo...
USING: io kernel math math.combinatorics math.parser math.ranges sequences tools.time ; IN: rosetta-code.largest-divisible : all-div? ( seq -- ? ) [ string>number ] [ string>digits ] bi [ mod ] with map sum 0 = ; : n-digit-all-div ( n -- seq ) "12346789" swap <combinations> [ [ all-div? ] filter-permu...
Ensure the translated Factor code behaves exactly like the original Go snippet.
package main import ( "fmt" "strconv" "strings" ) func divByAll(num int, digits []byte) bool { for _, digit := range digits { if num%int(digit-'0') != 0 { return false } } return true } func main() { magic := 9 * 8 * 7 high := 9876432 / magic * magic fo...
USING: io kernel math math.combinatorics math.parser math.ranges sequences tools.time ; IN: rosetta-code.largest-divisible : all-div? ( seq -- ? ) [ string>number ] [ string>digits ] bi [ mod ] with map sum 0 = ; : n-digit-all-div ( n -- seq ) "12346789" swap <combinations> [ [ all-div? ] filter-permu...
Write a version of this Go function in Factor with identical behavior.
package main import ( "fmt" "permute" ) func determinant(m [][]float64) (d float64) { p := make([]int, len(m)) for i := range p { p[i] = i } it := permute.Iter(p) for s := it(); s != 0; s = it() { pr := 1. for i, σ := range p { pr *= m[i][σ] } ...
USING: fry kernel math.combinatorics math.matrices sequences ; : permanent ( matrix -- x ) dup square-matrix? [ "Matrix must be square." throw ] unless [ dim first <iota> ] keep '[ [ _ nth nth ] map-index product ] map-permutations sum ;
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "fmt" "permute" ) func determinant(m [][]float64) (d float64) { p := make([]int, len(m)) for i := range p { p[i] = i } it := permute.Iter(p) for s := it(); s != 0; s = it() { pr := 1. for i, σ := range p { pr *= m[i][σ] } ...
USING: fry kernel math.combinatorics math.matrices sequences ; : permanent ( matrix -- x ) dup square-matrix? [ "Matrix must be square." throw ] unless [ dim first <iota> ] keep '[ [ _ nth nth ] map-index product ] map-permutations sum ;
Translate this program into Factor but keep the logic exactly as in Go.
package main import ( "fmt" "math/big" "rcu" ) func main() { count := 0 limit := 25 n := int64(17) repunit := big.NewInt(1111111111111111) t := new(big.Int) zero := new(big.Int) eleven := big.NewInt(11) hundred := big.NewInt(100) var deceptive []int64 for count < li...
USING: io kernel lists lists.lazy math math.functions math.primes prettyprint ; : repunit ( m -- n ) 10^ 1 - 9 / ; : composite ( -- list ) 4 lfrom [ prime? not ] lfilter ; : deceptive ( -- list ) composite [ [ 1 - repunit ] keep divisor? ] lfilter ; 10 deceptive ltake [ pprint bl ] leach nl
Preserve the algorithm and functionality while converting the code from Go to Factor.
package main import ( "fmt" "math/big" "rcu" ) func main() { count := 0 limit := 25 n := int64(17) repunit := big.NewInt(1111111111111111) t := new(big.Int) zero := new(big.Int) eleven := big.NewInt(11) hundred := big.NewInt(100) var deceptive []int64 for count < li...
USING: io kernel lists lists.lazy math math.functions math.primes prettyprint ; : repunit ( m -- n ) 10^ 1 - 9 / ; : composite ( -- list ) 4 lfrom [ prime? not ] lfilter ; : deceptive ( -- list ) composite [ [ 1 - repunit ] keep divisor? ] lfilter ; 10 deceptive ltake [ pprint bl ] leach nl
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import ( "fmt" "rcu" "strings" ) func main() { var numbers []int for n := 0; n < 1000; n++ { ns := fmt.Sprintf("%d", n) ds := fmt.Sprintf("%d", rcu.DigitSum(n, 10)) if strings.Contains(ns, ds) { numbers = append(numbers, n) } } fmt.P...
USING: grouping kernel math.text.utils present prettyprint sequences ; 1000 <iota> [ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter 8 group simple-table.
Maintain the same structure and functionality when rewriting this code in Factor.
package main import ( "fmt" "rcu" "strings" ) func main() { var numbers []int for n := 0; n < 1000; n++ { ns := fmt.Sprintf("%d", n) ds := fmt.Sprintf("%d", rcu.DigitSum(n, 10)) if strings.Contains(ns, ds) { numbers = append(numbers, n) } } fmt.P...
USING: grouping kernel math.text.utils present prettyprint sequences ; 1000 <iota> [ [ 1 digit-groups sum present ] [ present ] bi subseq? ] filter 8 group simple-table.
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "math/rand" "fmt" ) func main() { list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for i := 1; i <= 10; i++ { sattoloCycle(list) fmt.Println(list) } } func sattoloCycle(list []int) { for x := len(list) -1; x > 0; x-- { j := rand.Intn(x) list[x], list[j] = list[j], list[x] } }
USING: arrays io kernel literals math math.ranges prettyprint random sequences ; IN: rosetta-code.sattolo-cycle : (sattolo) ( seq -- seq' ) dup dup length 1 - 1 [a,b] [ dup iota random rot exchange ] with each ; : sattolo ( seq -- seq/seq' ) dup length 1 > [ (sattolo) ] when ; { { } { 10 } ...
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "math/rand" "fmt" ) func main() { list := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} for i := 1; i <= 10; i++ { sattoloCycle(list) fmt.Println(list) } } func sattoloCycle(list []int) { for x := len(list) -1; x > 0; x-- { j := rand.Intn(x) list[x], list[j] = list[j], list[x] } }
USING: arrays io kernel literals math math.ranges prettyprint random sequences ; IN: rosetta-code.sattolo-cycle : (sattolo) ( seq -- seq' ) dup dup length 1 - 1 [a,b] [ dup iota random rot exchange ] with each ; : sattolo ( seq -- seq/seq' ) dup length 1 > [ (sattolo) ] when ; { { } { 10 } ...
Produce a language-to-language conversion: from Go to Factor, same semantics.
package main import "fmt" func sieve(limit uint64) []bool { limit++ c := make([]bool, limit) c[0] = true c[1] = true p := uint64(3) for { p2 := p * p if p2 >= limit { break } for i := p2; i < limit; i += 2 * p { c[i] = true ...
USING: io kernel math math.parser math.primes.erato math.ranges sequences tools.memory.private ; : twin-pair-count ( n -- count ) [ 5 swap 2 <range> ] [ sieve ] bi [ over 2 - over [ marked-prime? ] 2bi@ and ] curry count ; "Search size: " write flush readln string>number twin-pair-count commas write " twin pr...
Change the following Go code into Factor without altering its purpose.
package main import "fmt" func sameDigits(n, b int) bool { f := n % b n /= b for n > 0 { if n%b != f { return false } n /= b } return true } func isBrazilian(n int) bool { if n < 7 { return false } if n%2 == 0 && n >= 8 { return true...
USING: combinators grouping io kernel lists lists.lazy math math.parser math.primes.lists math.ranges namespaces prettyprint prettyprint.config sequences ; : (brazilian?) ( n -- ? ) 2 over 2 - [a,b] [ >base all-equal? ] with find nip >boolean ; : brazilian? ( n -- ? ) { { [ dup 7 < ] [ drop f ] } ...
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import "fmt" func sameDigits(n, b int) bool { f := n % b n /= b for n > 0 { if n%b != f { return false } n /= b } return true } func isBrazilian(n int) bool { if n < 7 { return false } if n%2 == 0 && n >= 8 { return true...
USING: combinators grouping io kernel lists lists.lazy math math.parser math.primes.lists math.ranges namespaces prettyprint prettyprint.config sequences ; : (brazilian?) ( n -- ? ) 2 over 2 - [a,b] [ >base all-equal? ] with find nip >boolean ; : brazilian? ( n -- ? ) { { [ dup 7 < ] [ drop f ] } ...
Port the following code from Go to Factor with equivalent syntax and logic.
package main import ( "archive/tar" "compress/gzip" "flag" "io" "log" "os" "time" ) func main() { filename := flag.String("file", "TAPE.FILE", "filename within TAR") data := flag.String("data", "", "data for file") outfile := flag.String(...
USING: io.encodings.ascii io.files kernel system ; "Hello from Rosetta Code os windows? "tape.file" "/dev/tape" ? ascii set-file-contents
Generate an equivalent Factor version of this Go code.
package main import "fmt" type Func func(int) int type FuncFunc func(Func) Func type RecursiveFunc func (RecursiveFunc) Func func main() { fac := Y(almost_fac) fib := Y(almost_fib) fmt.Println("fac(10) = ", fac(10)) fmt.Println("fib(10) = ", fib(10)) } func Y(f FuncFunc) Func { g := func(r RecursiveFunc) Func ...
USING: fry kernel math ; IN: rosettacode.Y : Y ( quot -- quot ) '[ [ dup call call ] curry @ ] dup call ; inline : almost-fac ( quot -- quot ) '[ dup zero? [ drop 1 ] [ dup 1 - @ * ] if ] ; : almost-fib ( quot -- quot ) '[ dup 2 >= [ 1 2 [ - @ ] bi-curry@ bi + ] when ] ;
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "fmt" "strconv" ) func main() { var fact [12]uint64 fact[0] = 1 for n := uint64(1); n < 12; n++ { fact[n] = fact[n-1] * n } for b := 9; b <= 12; b++ { fmt.Printf("The factorions for base %d are:\n", b) for i := uint64(1); i < 1500000; i++...
USING: formatting io kernel math math.parser math.ranges memoize prettyprint sequences ; IN: rosetta-code.factorions MEMO: factorial ( n -- n : factorion? ( n base -- ? ) dupd >base string>digits [ factorial ] map-sum = ; : show-factorions ( limit base -- ) dup "The factorions for base %d are:\n" printf ...
Convert this Go snippet to Factor and keep its semantics consistent.
package main import ( "fmt" "strconv" ) func main() { var fact [12]uint64 fact[0] = 1 for n := uint64(1); n < 12; n++ { fact[n] = fact[n-1] * n } for b := 9; b <= 12; b++ { fmt.Printf("The factorions for base %d are:\n", b) for i := uint64(1); i < 1500000; i++...
USING: formatting io kernel math math.parser math.ranges memoize prettyprint sequences ; IN: rosetta-code.factorions MEMO: factorial ( n -- n : factorion? ( n base -- ? ) dupd >base string>digits [ factorial ] map-sum = ; : show-factorions ( limit base -- ) dup "The factorions for base %d are:\n" printf ...
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import "fmt" func sumDivisors(n int) int { sum := 0 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { sum += i j := n / i if j != i { sum += j } } i += k } return...
USING: grouping io math.primes.factors math.ranges prettyprint sequences ; "Sum of divisors for the first 100 positive integers:" print 100 [1,b] [ divisors sum ] map 10 group simple-table.
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(79) ix := 0 n := 1 count := 0 var pi []int for { if primes[ix] <= n { count++ if count == 22 { break } ix++ } n++ pi =...
USING: formatting grouping io lists math.primes math.primes.lists math.ranges math.statistics sequences ; 21 lprimes lnth [1,b) [ prime? ] cum-count 10 group [ [ "%2d " printf ] each nl ] each
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(79) ix := 0 n := 1 count := 0 var pi []int for { if primes[ix] <= n { count++ if count == 22 { break } ix++ } n++ pi =...
USING: formatting grouping io lists math.primes math.primes.lists math.ranges math.statistics sequences ; 21 lprimes lnth [1,b) [ prime? ] cum-count 10 group [ [ "%2d " printf ] each nl ] each
Translate the given Go code snippet into Factor without altering its behavior.
package main import ( "fmt" "sort" "strings" ) var count int = 0 func interactiveCompare(s1, s2 string) bool { count++ fmt.Printf("(%d) Is %s < %s? ", count, s1, s2) var response string _, err := fmt.Scanln(&response) return err == nil && strings.HasPrefix(response, "y") } func main...
USING: formatting io kernel math.order prettyprint qw sorting ; qw{ violet red green indigo blue yellow orange } [ "Is %s > %s? (y/n) " printf readln "y" = +gt+ +lt+ ? ] sort .
Change the programming language of this snippet from Go to Factor without modifying what it does.
package main import ( "fmt" "github.com/jbarham/primegen" "math" "math/big" "math/rand" "sort" "time" ) const ( maxCurves = 10000 maxRnd = 1 << 31 maxB1 = uint64(43 * 1e7) maxB2 = uint64(2 * 1e10) ) var ( zero = big.NewInt(0) one = big.NewInt(1) t...
USING: formatting io kernel lists lists.lazy math math.functions math.primes.factors sequences ; : lfermats ( -- list ) 0 lfrom [ [ 1 2 2 ] dip ^ ^ + ] lmap-lazy ; CHAR: ₀ 10 lfermats ltake list>array [ "First 10 Fermat numbers:" print [ dupd "F%c = %d\n" printf 1 + ] each drop nl ] [ "Factors of fir...
Write the same code in Factor as shown below in Go.
package main import ( "fmt" "sync" ) var a = []int{170, 45, 75, 90, 802, 24, 2, 66} var aMax = 1000 const bead = 'o' func main() { fmt.Println("before:", a) beadSort() fmt.Println("after: ", a) } func beadSort() { all := make([]byte, aMax*len(a)) abacus := make([][]byte, ...
USING: kernel math math.order math.vectors sequences ; : fill ( seq len -- newseq ) [ dup length ] dip swap - 0 <repetition> append ; : bead ( seq -- newseq ) dup 0 [ max ] reduce [ swap 1 <repetition> swap fill ] curry map [ ] [ v+ ] map-reduce ; : beadsort ( seq -- newseq ) bead bead ;
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import "fmt" func countDivisors(n int) int { count := 0 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { count++ j := n / i if j != i { count++ } } i += k } retu...
USING: assocs formatting io kernel math math.primes.factors math.ranges sequences sequences.extras ; ERROR: nonpositive n ; : (tau) ( n -- count ) group-factors values [ 1 + ] map-product ; inline : tau ( n -- count ) dup 0 > [ (tau) ] [ nonpositive ] if ; "Number of divisors for integers 1-100:" print nl " n ...
Port the following code from Go to Factor with equivalent syntax and logic.
package main import "fmt" func countDivisors(n int) int { count := 0 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { count++ j := n / i if j != i { count++ } } i += k } retu...
USING: assocs formatting io kernel math math.primes.factors math.ranges sequences sequences.extras ; ERROR: nonpositive n ; : (tau) ( n -- count ) group-factors values [ 1 + ] map-product ; inline : tau ( n -- count ) dup 0 > [ (tau) ] [ nonpositive ] if ; "Number of divisors for integers 1-100:" print nl " n ...
Produce a language-to-language conversion: from Go to Factor, same semantics.
package main import "fmt" func möbius(to int) []int { if to < 1 { to = 1 } mobs := make([]int, to+1) primes := []int{2} for i := 1; i <= to; i++ { j := i cp := 0 spf := false for _, p := range primes { if p > j { break ...
USING: formatting grouping io math.extras math.ranges sequences ; "First 199 terms of the Möbius sequence:" print 199 [1,b] [ mobius ] map " " prefix 20 group [ [ "%3s" printf ] each nl ] each
Write the same algorithm in Factor as shown in this Go implementation.
package main import "fmt" func main() { bf(10, `++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++ ++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>> >+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++. <+++++++.--------.<<<<<+.<+++.---.`) } func bf(dLen int, is string) { ds := make([]byt...
USE: brainf*** "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." run-brainf***
Please provide an equivalent version of this Go code in Factor.
package main import "fmt" func main() { bf(10, `++++++++++[>+>+++>++++>+++++++>++++++++>+++++++++>++ ++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>+.>>> >+..<.<++++++++.>>>+.<<+.<<<<++++.<++.>>>+++++++.>>>.+++. <+++++++.--------.<<<<<+.<+++.---.`) } func bf(dLen int, is string) { ds := make([]byt...
USE: brainf*** "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++." run-brainf***
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import ( "fmt" "rcu" ) func contains(a []int, v int) bool { for _, e := range a { if e == v { return true } } return false } func main() { const limit = 50 cpt := []int{1, 2} for { m := 1 l := len(cpt) for contains(cpt, ...
USING: combinators.short-circuit.smart formatting grouping io kernel make math prettyprint sequences sets ; : coprime? ( m n -- ? ) simple-gcd 1 = ; : coprime-both? ( m n o -- ? ) '[ _ coprime? ] both? ; : triplet? ( hs m n o -- ? ) { [ coprime-both? nip ] [ 2nip swap in? not ] } && ; : next ( hs m n -- hs' m' ...
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "fmt" "rcu" ) func contains(a []int, v int) bool { for _, e := range a { if e == v { return true } } return false } func main() { const limit = 50 cpt := []int{1, 2} for { m := 1 l := len(cpt) for contains(cpt, ...
USING: combinators.short-circuit.smart formatting grouping io kernel make math prettyprint sequences sets ; : coprime? ( m n -- ? ) simple-gcd 1 = ; : coprime-both? ( m n o -- ? ) '[ _ coprime? ] both? ; : triplet? ( hs m n o -- ? ) { [ coprime-both? nip ] [ 2nip swap in? not ] } && ; : next ( hs m n -- hs' m' ...
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" "math/big" ) func main() { zero := big.NewInt(0) one := big.NewInt(1) for k := int64(2); k <= 10; k += 2 { bk := big.NewInt(k) fmt.Println("The first 50 Curzon numbers using a base of", k, ":") count := 0 n := int64(1) pow := big....
USING: grouping interpolate io kernel make math math.functions prettyprint ranges sequences ; : curzon? ( k n -- ? ) [ ^ 1 + ] 2keep * 1 + divisor? ; : next ( k n -- k n' ) [ 2dup curzon? ] [ 1 + ] do until ; : curzon ( k -- seq ) 1 [ 50 [ dup , next ] times ] { } make 2nip ; : curzon. ( k -- ) dup [I Curzo...
Port the following code from Go to Factor with equivalent syntax and logic.
package main import ( "fmt" "math/big" ) func main() { zero := big.NewInt(0) one := big.NewInt(1) for k := int64(2); k <= 10; k += 2 { bk := big.NewInt(k) fmt.Println("The first 50 Curzon numbers using a base of", k, ":") count := 0 n := int64(1) pow := big....
USING: grouping interpolate io kernel make math math.functions prettyprint ranges sequences ; : curzon? ( k n -- ? ) [ ^ 1 + ] 2keep * 1 + divisor? ; : next ( k n -- k n' ) [ 2dup curzon? ] [ 1 + ] do until ; : curzon ( k -- seq ) 1 [ 50 [ dup , next ] times ] { } make 2nip ; : curzon. ( k -- ) dup [I Curzo...
Please provide an equivalent version of this Go code in Factor.
package main import "fmt" func mertens(to int) ([]int, int, int) { if to < 1 { to = 1 } merts := make([]int, to+1) primes := []int{2} var sum, zeros, crosses int for i := 1; i <= to; i++ { j := i cp := 0 spf := false for _, p := range primes { ...
USING: formatting grouping io kernel math math.extras math.ranges math.statistics prettyprint sequences ; : mertens-upto ( n -- seq ) [1,b] [ mobius ] map cum-sum ; "The first 199 terms of the Mertens sequence:" print 199 mertens-upto " " prefix 20 group [ [ "%3s" printf ] each nl ] each nl "In the first 1,000 ter...
Translate the given Go code snippet into Factor without altering its behavior.
package main import "fmt" func prodDivisors(n int) int { prod := 1 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { prod *= i j := n / i if j != i { prod *= j } } i += k } re...
USING: grouping io math.primes.factors math.ranges prettyprint sequences ; "Product of divisors for the first 50 positive integers:" print 50 [1,b] [ divisors product ] map 5 group simple-table.
Ensure the translated Factor code behaves exactly like the original Go snippet.
package main import "fmt" func prodDivisors(n int) int { prod := 1 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { prod *= i j := n / i if j != i { prod *= j } } i += k } re...
USING: grouping io math.primes.factors math.ranges prettyprint sequences ; "Product of divisors for the first 50 positive integers:" print 50 [1,b] [ divisors product ] map 5 group simple-table.
Please provide an equivalent version of this Go code in Factor.
package cards import ( "math/rand" ) type Suit uint8 const ( Spade Suit = 3 Heart Suit = 2 Diamond Suit = 1 Club Suit = 0 ) func (s Suit) String() string { const suites = "CDHS" return suites[s : s+1] } type Rank uint8 const ( Ace Rank = 1 Two Rank = 2 Three Rank = 3 Four Rank = 4 Fiv...
USING: formatting grouping io kernel math qw random sequences vectors ; IN: rosetta-code.playing-cards CONSTANT: pips qw{ A 2 3 4 5 6 7 8 9 10 J Q K } CONSTANT: suits qw{ ♥ ♣ ♦ ♠ } : <deck> ( -- vec ) 52 <iota> >vector ; : card>str ( n -- str ) 13 /mod [ suits nth ] [ pips nth ] bi* prepend ; : print-deck ...
Write a version of this Go function in Factor with identical behavior.
package main import ( "fmt" "rcu" ) func main() { pairs := [][2]int{{21, 15}, {17, 23}, {36, 12}, {18, 29}, {60, 15}} fmt.Println("The following pairs of numbers are coprime:") for _, pair := range pairs { if rcu.Gcd(pair[0], pair[1]) == 1 { fmt.Println(pair) } } }
USING: io kernel math prettyprint sequences ; : coprime? ( seq -- ? ) [ ] [ simple-gcd ] map-reduce 1 = ; { { 21 15 } { 17 23 } { 36 12 } { 18 29 } { 60 15 } { 21 22 25 31 143 } } [ dup pprint coprime? [ " Coprime" write ] when nl ] each
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import "fmt" func gcd(n, k int) int { if n < k || k < 1 { panic("Need n >= k and k >= 1") } s := 1 for n&1 == 0 && k&1 == 0 { n >>= 1 k >>= 1 s <<= 1 } t := n if n&1 != 0 { t = -k } for t != 0 { for t&1 == 0 { ...
USING: formatting kernel lists lists.lazy math math.primes.factors ; : perfect? ( n -- ? ) [ 0 ] dip dup [ dup 2 < ] [ totient tuck [ + ] 2dip ] until drop = ; 20 1 lfrom [ perfect? ] lfilter ltake list>array "%[%d, %]\n" printf
Change the programming language of this snippet from Go to Factor without modifying what it does.
package main import "fmt" func gcd(n, k int) int { if n < k || k < 1 { panic("Need n >= k and k >= 1") } s := 1 for n&1 == 0 && k&1 == 0 { n >>= 1 k >>= 1 s <<= 1 } t := n if n&1 != 0 { t = -k } for t != 0 { for t&1 == 0 { ...
USING: formatting kernel lists lists.lazy math math.primes.factors ; : perfect? ( n -- ? ) [ 0 ] dip dup [ dup 2 < ] [ totient tuck [ + ] 2dip ] until drop = ; 20 1 lfrom [ perfect? ] lfilter ltake list>array "%[%d, %]\n" printf
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" big "github.com/ncw/gmp" ) var two = big.NewInt(2) func a(n uint) int { one := big.NewInt(1) p := new(big.Int).Lsh(one, 1 << n) p.Sub(p, one) for k := 1; ; k += 2 { if p.ProbablyPrime(15) { return k } p.Sub(p, two) } } func ...
USING: io kernel lists lists.lazy math math.primes prettyprint ; : useful ( -- list ) 1 lfrom [ 2^ 2^ 1 lfrom [ - prime? ] with lfilter car ] lmap-lazy ; 10 useful ltake [ pprint bl ] leach nl
Translate the given Go code snippet into Factor without altering its behavior.
package main import ( "fmt" big "github.com/ncw/gmp" ) var two = big.NewInt(2) func a(n uint) int { one := big.NewInt(1) p := new(big.Int).Lsh(one, 1 << n) p.Sub(p, one) for k := 1; ; k += 2 { if p.ProbablyPrime(15) { return k } p.Sub(p, two) } } func ...
USING: io kernel lists lists.lazy math math.primes prettyprint ; : useful ( -- list ) 1 lfrom [ 2^ 2^ 1 lfrom [ - prime? ] with lfilter car ] lmap-lazy ; 10 useful ltake [ pprint bl ] leach nl
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "fmt" "math/big" ) func main() { limit := 100 last := 12 unsigned := true l := make([][]*big.Int, limit+1) for n := 0; n <= limit; n++ { l[n] = make([]*big.Int, limit+1) for k := 0; k <= limit; k++ { l[n][k] = new(big.Int) } ...
USING: combinators combinators.short-circuit formatting infix io kernel locals math math.factorials math.ranges prettyprint sequences ; IN: rosetta-code.lah-numbers INFIX:: (lah) ( n k -- m ) ( factorial(n) * factorial(n-1) ) / ( factorial(k) * factorial(k-1) ) / factorial(n-k) ; :: lah ( n k -- m ) { ...
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "fmt" "math/big" ) func main() { limit := 100 last := 12 unsigned := true l := make([][]*big.Int, limit+1) for n := 0; n <= limit; n++ { l[n] = make([]*big.Int, limit+1) for k := 0; k <= limit; k++ { l[n][k] = new(big.Int) } ...
USING: combinators combinators.short-circuit formatting infix io kernel locals math math.factorials math.ranges prettyprint sequences ; IN: rosetta-code.lah-numbers INFIX:: (lah) ( n k -- m ) ( factorial(n) * factorial(n-1) ) / ( factorial(k) * factorial(k-1) ) / factorial(n-k) ; :: lah ( n k -- m ) { ...
Translate this program into Factor but keep the logic exactly as in Go.
package main import "fmt" func twoSum(a []int, targetSum int) (int, int, bool) { len := len(a) if len < 2 { return 0, 0, false } for i := 0; i < len - 1; i++ { if a[i] <= targetSum { for j := i + 1; j < len; j++ { sum := a[i] + a[j] if sum ==...
USING: combinators fry kernel locals math prettyprint sequences ; IN: rosetta-code.two-sum :: two-sum ( seq target -- index-pair ) 0 seq length 1 - :> ( x x y [ seq nth ] bi@ + :> sum { { [ sum target = x y = or ] [ f ] } { [ sum target > ] [ y 1 - y [ x 1 + x } ...
Change the programming language of this snippet from Go to Factor without modifying what it does.
package main import "fmt" func twoSum(a []int, targetSum int) (int, int, bool) { len := len(a) if len < 2 { return 0, 0, false } for i := 0; i < len - 1; i++ { if a[i] <= targetSum { for j := i + 1; j < len; j++ { sum := a[i] + a[j] if sum ==...
USING: combinators fry kernel locals math prettyprint sequences ; IN: rosetta-code.two-sum :: two-sum ( seq target -- index-pair ) 0 seq length 1 - :> ( x x y [ seq nth ] bi@ + :> sum { { [ sum target = x y = or ] [ f ] } { [ sum target > ] [ y 1 - y [ x 1 + x } ...
Translate this program into Factor but keep the logic exactly as in Go.
package main import ( "fmt" "strconv" ) func isPrime(n int) bool { switch { case n < 2: return false case n%2 == 0: return n == 2 case n%3 == 0: return n == 3 default: d := 5 for d*d <= n { if n%d == 0 { return false ...
USING: assocs formatting io kernel lists lists.lazy lists.lazy.examples math math.functions math.primes math.ranges math.text.utils prettyprint sequences tools.memory.private ; : one-offs ( n -- seq ) dup 1 digit-groups [ swapd 10^ [ * ] keep [ - ] dip 2dup [ 9 * ] [ + ] [ <range> ] tri* ] with...
Port the following code from Go to Factor with equivalent syntax and logic.
package main import "fmt" func countDivisors(n int) int { count := 0 i := 1 k := 2 if n%2 == 0 { k = 1 } for i*i <= n { if n%i == 0 { count++ j := n / i if j != i { count++ } } i += k } retu...
USING: assocs grouping io kernel lists lists.lazy math math.functions math.primes.factors prettyprint sequences sequences.extras ; : tau ( n -- count ) group-factors values [ 1 + ] map-product ; : tau? ( n -- ? ) dup tau divisor? ; : taus ( -- list ) 1 lfrom [ tau? ] lfilter ; "The first 100 tau numbers are:" prin...
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" big "github.com/ncw/gmp" "time" ) func sieve(limit int) []bool { limit++ c := make([]bool, limit) c[0] = true c[1] = true p := 3 for { p2 := p * p if p2 >= limit { break } for i := p2; i < limit; i...
USING: kernel lists lists.lazy math math.primes.lists prettyprint ; : digit-sum ( n -- sum ) 0 swap [ 10 /mod rot + swap ] until-zero ; : lprimes25 ( -- list ) lprimes [ digit-sum 25 = ] lfilter ; lprimes25 [ 5,000 < ] lwhile [ . ] leach
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "fmt" big "github.com/ncw/gmp" "time" ) func sieve(limit int) []bool { limit++ c := make([]bool, limit) c[0] = true c[1] = true p := 3 for { p2 := p * p if p2 >= limit { break } for i := p2; i < limit; i...
USING: kernel lists lists.lazy math math.primes.lists prettyprint ; : digit-sum ( n -- sum ) 0 swap [ 10 /mod rot + swap ] until-zero ; : lprimes25 ( -- list ) lprimes [ digit-sum 25 = ] lfilter ; lprimes25 [ 5,000 < ] lwhile [ . ] leach
Convert this Go snippet to Factor and keep its semantics consistent.
package main import ( "fmt" "sort" "strconv" ) func combrep(n int, lst []byte) [][]byte { if n == 0 { return [][]byte{nil} } if len(lst) == 0 { return nil } r := combrep(n, lst[1:]) for _, x := range combrep(n-1, lst) { r = append(r, append(x, lst[0])) }...
USING: formatting io kernel math math.combinatorics math.functions math.ranges sequences sequences.extras ; : digits>number ( seq -- n ) reverse 0 [ 10^ * + ] reduce-index ; "Numbers whose digits are prime and sum to 13:" print { 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with map-concat [ digits>number ]...
Port the provided Go code into Factor while preserving the original functionality.
package main import ( "fmt" "sort" "strconv" ) func combrep(n int, lst []byte) [][]byte { if n == 0 { return [][]byte{nil} } if len(lst) == 0 { return nil } r := combrep(n, lst[1:]) for _, x := range combrep(n-1, lst) { r = append(r, append(x, lst[0])) }...
USING: formatting io kernel math math.combinatorics math.functions math.ranges sequences sequences.extras ; : digits>number ( seq -- n ) reverse 0 [ 10^ * + ] reduce-index ; "Numbers whose digits are prime and sum to 13:" print { 2 3 5 7 } 3 6 [a,b] [ selections [ sum 13 = ] filter ] with map-concat [ digits>number ]...
Convert this Go snippet to Factor and keep its semantics consistent.
package main import "fmt" type cds struct { i int s string b []byte m map[int]bool } func (c cds) deepcopy() *cds { r := &cds{c.i, c.s, append([]byte{}, c.b...), make(map[int]bool)} for k, v := range c.m { r.m[k] = v } return r } ...
USING: accessors arrays io kernel named-tuples prettyprint sequences sequences.deep ; TUPLE: foo bar baz ; INSTANCE: foo named-tuple V{ 1 2 3 } V{ 4 5 6 } [ clone ] bi@ foo boa dup >array [ clone ] deep-map T{ foo } like "Before modification:" print [ [ . ] bi@ ] 2keep nl [ -1 suffix "After modificatio...
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import "fmt" type cds struct { i int s string b []byte m map[int]bool } func (c cds) deepcopy() *cds { r := &cds{c.i, c.s, append([]byte{}, c.b...), make(map[int]bool)} for k, v := range c.m { r.m[k] = v } return r } ...
USING: accessors arrays io kernel named-tuples prettyprint sequences sequences.deep ; TUPLE: foo bar baz ; INSTANCE: foo named-tuple V{ 1 2 3 } V{ 4 5 6 } [ clone ] bi@ foo boa dup >array [ clone ] deep-map T{ foo } like "Before modification:" print [ [ . ] bi@ ] 2keep nl [ -1 suffix "After modificatio...
Produce a language-to-language conversion: from Go to Factor, same semantics.
package main import ( "fmt" big "github.com/ncw/gmp" "strings" ) func isPrime(n int) bool { switch { case n < 2: return false case n%2 == 0: return n == 2 case n%3 == 0: return n == 3 default: d := 5 for d*d <= n { if n%d == 0 { ...
USING: combinators.short-circuit formatting io kernel lists lists.lazy math math.combinatorics math.functions math.parser math.primes sequences sequences.extras ; : candidates ( -- list ) L{ "2" "3" "5" "7" } 2 lfrom [ "1379" swap selections >list ] lmap-lazy lconcat lappend ; : circular-prime? ( str -- ?...
Write the same code in Factor as shown below in Go.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(101) var frobenius []int for i := 0; i < len(primes)-1; i++ { frob := primes[i]*primes[i+1] - primes[i] - primes[i+1] if frob >= 10000 { break } frobenius = append(frobenius, frob) ...
USING: io kernel math math.primes prettyprint ; "Frobenius numbers < 10,000:" print 2 3 [ [ nip dup next-prime ] [ * ] [ [ - ] dip - ] 2tri dup 10,000 < ] [ . ] while 3drop
Port the following code from Go to Factor with equivalent syntax and logic.
package main import ( "fmt" "rcu" ) func main() { primes := rcu.Primes(101) var frobenius []int for i := 0; i < len(primes)-1; i++ { frob := primes[i]*primes[i+1] - primes[i] - primes[i+1] if frob >= 10000 { break } frobenius = append(frobenius, frob) ...
USING: io kernel math math.primes prettyprint ; "Frobenius numbers < 10,000:" print 2 3 [ [ nip dup next-prime ] [ * ] [ [ - ] dip - ] 2tri dup 10,000 < ] [ . ] while 3drop
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import "fmt" var a = []int{170, 45, 75, -90, -802, 24, 2, 66} func main() { fmt.Println("before:", a) if len(a) > 1 && !recurse(len(a) - 1) { panic("sorted permutation not found!") } fmt.Println("after: ", a) } func recurse(last int) bool { ...
USING: grouping io math.combinatorics math.order prettyprint ; IN: rosetta-code.permutation-sort : permutation-sort ( seq -- seq' ) [ [ before=? ] monotonic? ] find-permutation ; { 10 2 6 8 1 4 3 } permutation-sort . "apple" permutation-sort print
Port the following code from Go to Factor with equivalent syntax and logic.
package main import "fmt" func main() { fmt.Println(root(3, 8)) fmt.Println(root(3, 9)) fmt.Println(root(2, 2e18)) } func root(N, X int) int { for r := 1; ; { x := X for i := 1; i < N; i++ { x /= r } x -= r Δ...
USING: io kernel locals math math.functions math.order prettyprint sequences ; :: (root) ( a b -- n ) a 1 - 1 :> ( a1 c [| x | a1 x * b x a1 ^ /i + a /i ] :> f c f call :> d d f call :> e [ c { d e } member? ] [ d c ] until d e min ; : root ( a b -- n ) dup 2 < [ nip ] [ (root) ] i...
Preserve the algorithm and functionality while converting the code from Go to Factor.
package main import "fmt" func main() { fmt.Println(root(3, 8)) fmt.Println(root(3, 9)) fmt.Println(root(2, 2e18)) } func root(N, X int) int { for r := 1; ; { x := X for i := 1; i < N; i++ { x /= r } x -= r Δ...
USING: io kernel locals math math.functions math.order prettyprint sequences ; :: (root) ( a b -- n ) a 1 - 1 :> ( a1 c [| x | a1 x * b x a1 ^ /i + a /i ] :> f c f call :> d d f call :> e [ c { d e } member? ] [ d c ] until d e min ; : root ( a b -- n ) dup 2 < [ nip ] [ (root) ] i...
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "fmt" "math/big" "rcu" "sort" ) func main() { primes := rcu.Primes(379) primorial := big.NewInt(1) var fortunates []int bPrime := new(big.Int) for _, prime := range primes { bPrime.SetUint64(uint64(prime)) primorial.Mul(primorial, bPrime) ...
USING: grouping io kernel math math.factorials math.primes math.ranges prettyprint sequences sets sorting ; "First 50 distinct fortunate numbers:" print 75 [1,b] [ primorial dup next-prime 2dup - abs 1 = [ next-prime ] when - abs ] map members natural-sort 50 head 10 group simple-table.
Convert this Go snippet to Factor and keep its semantics consistent.
package main import "fmt" func MeaningOfLife() int { return 42 } func libMain() { fmt.Println("The meaning of life is", MeaningOfLife()) }
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ; IN: syntax : include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ; SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import ( "fmt" "go/ast" "go/parser" "go/token" "io/ioutil" "os" "sort" ) func main() { if len(os.Args) != 2 { fmt.Println("usage ff <go source filename>") return } src, err := ioutil.ReadFile(os.Args[1]) if err != nil { fmt.Println(err) ...
USING: accessors kernel math.statistics prettyprint sequences sequences.deep source-files vocabs words ; "resource:core/sequences/sequences.factor" "sequences" [ path>source-file top-level-form>> ] [ vocab-words [ def>> ] [ ] map-as ] bi* compose [ word? ] deep-filter sorted-histogram <reversed> 7 head .
Port the provided Go code into Factor while preserving the original functionality.
package main import "fmt" func isPrime(n int) bool { switch { case n < 2: return false case n%2 == 0: return n == 2 case n%3 == 0: return n == 3 default: d := 5 for d*d <= n { if n%d == 0 { return false } d...
USING: math math.primes prettyprint sequences ; : digital-root ( m -- n ) 1 - 9 mod 1 + ; 500 1000 primes-between [ digital-root prime? ] filter .
Generate a Factor translation of this Go snippet without changing its computational steps.
package main import "fmt" func isPrime(n int) bool { switch { case n < 2: return false case n%2 == 0: return n == 2 case n%3 == 0: return n == 3 default: d := 5 for d*d <= n { if n%d == 0 { return false } d...
USING: math math.primes prettyprint sequences ; : digital-root ( m -- n ) 1 - 9 mod 1 + ; 500 1000 primes-between [ digital-root prime? ] filter .
Ensure the translated Factor code behaves exactly like the original Go snippet.
package main import ( "fmt" "time" ) func main() { var year int var t time.Time var lastDay = [12]int { 31,29,31,30,31,30,31,31,30,31,30,31 } for { fmt.Print("Please select a year: ") _, err := fmt.Scanf("%d", &year) if err != nil { fmt.Println(err) continue } else { break } } fmt.Prin...
USING: calendar calendar.format command-line io kernel math math.parser sequences ; IN: rosetta-code.last-sunday : parse-year ( -- ts ) (command-line) second string>number <year> ; : print-last-sun ( ts -- ) last-sunday-of-month (timestamp>ymd) nl ; : inc-month ( ts -- ts' ) 1 months time+ ; : process...
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "fmt" "time" ) func main() { var year int var t time.Time var lastDay = [12]int { 31,29,31,30,31,30,31,31,30,31,30,31 } for { fmt.Print("Please select a year: ") _, err := fmt.Scanf("%d", &year) if err != nil { fmt.Println(err) continue } else { break } } fmt.Prin...
USING: calendar calendar.format command-line io kernel math math.parser sequences ; IN: rosetta-code.last-sunday : parse-year ( -- ts ) (command-line) second string>number <year> ; : print-last-sun ( ts -- ) last-sunday-of-month (timestamp>ymd) nl ; : inc-month ( ts -- ts' ) 1 months time+ ; : process...
Maintain the same structure and functionality when rewriting this code in Factor.
package main import ( "fmt" "math/rand" "time" ) type matrix [][]int func shuffle(row []int, n int) { rand.Shuffle(n, func(i, j int) { row[i], row[j] = row[j], row[i] }) } func latinSquare(n int) { if n <= 0 { fmt.Println("[]\n") return } latin := make(matrix,...
USING: arrays combinators.extras fry io kernel math.matrices prettyprint random sequences sets ; IN: rosetta-code.random-latin-squares : rand-permutation ( n -- seq ) <iota> >array randomize ; : ls? ( n -- ? ) [ all-unique? ] column-map t [ and ] reduce ; : (ls) ( n -- m ) dup '[ _ rand-permutation ] replicate ; : ls ...
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "bufio" "fmt" "log" "os" "sort" "strings" ) func check(err error) { if err != nil { log.Fatal(err) } } func readWords(fileName string) []string { file, err := os.Open(fileName) check(err) defer file.Close() var words []string scanner :...
USING: combinators.short-circuit fry grouping hash-sets http.client kernel math prettyprint sequences sequences.extras sets sorting splitting ; "https://www.mit.edu/~ecprice/wordlist.10000" http-get nip "\n" split [ { [ length 3 < ] [ all-equal? ] } 1|| ] reject [ [ all-rotations ] map ] [ >hash-set ] bi '[ [ _ in? ] ...
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import ( "fmt" "sort" "strconv" "strings" ) func fairshare(n, base int) []int { res := make([]int, n) for i := 0; i < n; i++ { j := i sum := 0 for j > 0 { sum += j % base j /= base } res[i] = sum % base } retu...
USING: formatting kernel math math.parser sequences ; : nth-fairshare ( n base -- m ) [ >base string>digits sum ] [ mod ] bi ; : <fairshare> ( n base -- seq ) [ nth-fairshare ] curry { } map-integers ; { 2 3 5 11 } [ 25 over <fairshare> "%2d -> %u\n" printf ] each
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "fmt" "strconv" ) func uabs(a, b uint64) uint64 { if a > b { return a - b } return b - a } func isEsthetic(n, b uint64) bool { if n == 0 { return false } i := n % b n /= b for n > 0 { j := n % b if uabs(i, j) != 1 { ...
USING: combinators deques dlists formatting grouping io kernel locals make math math.order math.parser math.ranges math.text.english prettyprint sequences sorting strings ; :: bfs ( from to num base -- ) DL{ } clone :> q base 1 - :> ld num q push-front [ q deque-empty? ] [ q pop-back :> ste...
Change the programming language of this snippet from Go to Factor without modifying what it does.
package main import ( "fmt" "rcu" "sort" "strconv" ) func combinations(a []int, k int) [][]int { n := len(a) c := make([]int, k) var combs [][]int var combine func(start, end, index int) combine = func(start, end, index int) { if index == k { t := make([]int, le...
USING: grouping grouping.extras math math.combinatorics math.functions math.primes math.ranges prettyprint sequences sequences.extras ; 9 1 [a,b] all-subsets [ reverse 0 [ 10^ * + ] reduce-index ] [ prime? ] map-filter 10 "" pad-groups 10 group simple-table.
Transform the following Go implementation into Factor, maintaining the same output and logic.
package main import ( "fmt" "math/rand" "sort" "time" ) func main() { s := rand.NewSource(time.Now().UnixNano()) r := rand.New(s) for { var values [6]int vsum := 0 for i := range values { var numbers [4]int for j := range numbers { ...
USING: combinators.short-circuit dice formatting io kernel math math.statistics qw sequences ; IN: rosetta-code.rpg-attributes-generator CONSTANT: stat-names qw{ Str Dex Con Int Wis Cha } : attribute ( -- n ) 4 [ ROLL: 1d6 ] replicate 3 <iota> kth-largests sum ; : stats ( -- seq ) 6 [ attribute ] replicate ;...
Please provide an equivalent version of this Go code in Factor.
package main import "fmt" func main() { s := []int{1, 2, 2, 3, 4, 4, 5} for i := 0; i < len(s); i++ { curr := s[i] var prev int if i > 0 && curr == prev { fmt.Println(i) } prev = curr } var prev int for i := 0; i < len(s); i...
USING: kernel math prettyprint sequences ; [let { 1 2 2 3 4 4 5 } :> s s length <iota> [| i | i s nth -1 :> ( curr prev i 0 > curr prev = and [ i . ] when curr prev ] each ]
Change the following Go code into Factor without altering its purpose.
package main import "fmt" func countDivisors(n int) int { count := 0 for i := 1; i*i <= n; i++ { if n%i == 0 { if i == n/i { count++ } else { count += 2 } } } return count } func main() { const max = 15 seq :=...
USING: fry kernel lists lists.lazy math math.primes.factors prettyprint sequences ; : A005179 ( -- list ) 1 lfrom [ 1 swap '[ dup divisors length _ = ] [ 1 + ] until ] lmap-lazy ; 15 A005179 ltake list>array .
Rewrite the snippet below in Factor so it works the same as the original Go code.
package main import ( "bufio" "errors" "fmt" "math" "os" "regexp" "strconv" "strings" ) func main() { fmt.Println("Numbers please separated by space/commas:") sc := bufio.NewScanner(os.Stdin) sc.Scan() s, n, min, max, err := spark(sc.Text()) if err != nil { ...
USING: formatting kernel math math.order math.parser math.statistics sequences splitting ; : sparkline-index ( v min max -- i ) [ drop - 8 * ] [ swap - /i ] 2bi 0 7 clamp 9601 + ; : (sparkline) ( seq -- new-seq ) dup minmax [ sparkline-index ] 2curry "" map-as ; : sparkline ( str -- new-str ) ", " split ...
Produce a language-to-language conversion: from Go to Factor, same semantics.
package main import ( "fmt" "math" ) func main() { pow := 1 for p := 0; p < 5; p++ { low := int(math.Ceil(math.Sqrt(float64(pow)))) if low%2 == 0 { low++ } pow *= 10 high := int(math.Sqrt(float64(pow))) var oddSq []int for i := low; i...
USING: io math math.functions math.ranges prettyprint sequences ; 11 1000 sqrt 2 <range> [ bl ] [ sq pprint ] interleave nl
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "bytes" "fmt" "io/ioutil" "log" "sort" "strings" "unicode/utf8" ) func main() { wordList := "unixdict.txt" b, err := ioutil.ReadFile(wordList) if err != nil { log.Fatal("Error reading file") } bwords := bytes.Fields(b) var words []strin...
USING: formatting grouping hash-sets io.encodings.ascii io.files kernel literals math math.matrices sequences sequences.extras sets strings ; << CONSTANT: words $[ "unixdict.txt" ascii file-lines ] >> CONSTANT: wordset $[ words >hash-set ] words [ length 9 < ] reject ...
Convert the following code from Go to Factor, ensuring the logic remains intact.
package main import ( "fmt" "rcu" ) func main() { for i := 1; i < 100; i++ { if !rcu.IsPrime(rcu.DigitSum(i*i, 10)) { continue } if rcu.IsPrime(rcu.DigitSum(i*i*i, 10)) { fmt.Printf("%d ", i) } } fmt.Println() }
USING: kernel math math.functions math.primes math.text.utils prettyprint sequences ; 100 <iota> [ [ sq ] [ 3 ^ ] bi [ 1 digit-groups sum prime? ] both? ] filter .
Produce a functionally identical Factor code for the snippet given in Go.
package main import ( "bufio" "fmt" "log" "os" "strconv" "strings" ) func check(err error) { if err != nil { log.Fatal(err) } } func main() { scanner := bufio.NewScanner(os.Stdin) n := 0 for n < 1 || n > 5 { fmt.Print("How many integer variables do you want...
42 readln set
Rewrite this program in Factor while keeping its functionality equivalent to the Go version.
package main import ( "fmt" "rcu" "sort" ) func main() { primes := rcu.Primes(333) var oss []int for i := 1; i < len(primes)-1; i++ { for j := i + 1; j < len(primes); j++ { n := primes[i] * primes[j] if n >= 1000 { break } ...
USING: combinators.short-circuit formatting grouping io kernel math.primes.factors math.ranges prettyprint sequences sets ; : sq-free-semiprime? ( n -- ? ) factors { [ length 2 = ] [ all-unique? ] } 1&& ; : odd-sfs-upto ( n -- seq ) 1 swap 2 <range> [ sq-free-semiprime? ] filter ; 999 odd-sfs-upto dup length...
Produce a language-to-language conversion: from Go to Factor, same semantics.
package main import ( "fmt" "rcu" "sort" ) func main() { primes := rcu.Primes(333) var oss []int for i := 1; i < len(primes)-1; i++ { for j := i + 1; j < len(primes); j++ { n := primes[i] * primes[j] if n >= 1000 { break } ...
USING: combinators.short-circuit formatting grouping io kernel math.primes.factors math.ranges prettyprint sequences sets ; : sq-free-semiprime? ( n -- ? ) factors { [ length 2 = ] [ all-unique? ] } 1&& ; : odd-sfs-upto ( n -- seq ) 1 swap 2 <range> [ sq-free-semiprime? ] filter ; 999 odd-sfs-upto dup length...
Convert this Go block to Factor, preserving its control flow and logic.
package main import ( "fmt" "rcu" ) func nonDescending(p int) bool { var digits []int for p > 0 { digits = append(digits, p%10) p = p / 10 } for i := 0; i < len(digits)-1; i++ { if digits[i+1] > digits[i] { return false } } return true } fun...
USING: grouping lists lists.lazy math math.primes.lists present prettyprint ; lprimes [ present [ <= ] monotonic? ] lfilter [ 1000 < ] lwhile [ . ] leach
Keep all operations the same but rewrite the snippet in Factor.
package main import ( "fmt" "rcu" ) func nonDescending(p int) bool { var digits []int for p > 0 { digits = append(digits, p%10) p = p / 10 } for i := 0; i < len(digits)-1; i++ { if digits[i+1] > digits[i] { return false } } return true } fun...
USING: grouping lists lists.lazy math math.primes.lists present prettyprint ; lprimes [ present [ <= ] monotonic? ] lfilter [ 1000 < ] lwhile [ . ] leach