September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,14 @@
begin
integer a, b;
write( "Enter 2 integers> " );
read( a, b );
write( "a + b: ", a + b ); % addition %
write( "a - b: ", a - b ); % subtraction %
write( "a * b: ", a * b ); % multiplication %
write( "a / b: ", a div b ); % integer division %
write( "a mod b: ", a rem b ); % modulo %
% the ** operator returns a real result even with integer operands %
% ( the right-hand operand must always be an integer, the left-hand %
% operand can be integer, real or complex ) %
write( "a ^ b: ", round( a ** b ) )
end.

View file

@ -0,0 +1,12 @@
' Arthimetic/Integer
DECLARE a%, b%
INPUT "Enter integer A: ", a%
INPUT "Enter integer B: ", b%
PRINT
PRINT a%, " + ", b%, " is ", a% + b%
PRINT a%, " - ", b%, " is ", a% - b%
PRINT a%, " * ", b%, " is ", a% * b%
PRINT a%, " / ", b%, " is ", a% / b%, ", trucation toward zero"
PRINT "MOD(", a%, ", ", b%, ") is ", MOD(a%, b%), ", same sign as first operand"
PRINT "POW(", a%, ", ", b%, ") is ", INT(POW(a%, b%))

View file

@ -0,0 +1,8 @@
10 INPUT "ENTER A NUMBER"; A%
20 INPUT "ENTER ANOTHER NUMBER"; B%
30 PRINT "ADDITION:";A%;"+";B%;"=";A%+B%
40 PRINT "SUBTRACTION:";A%;"-";B%;"=";A%-B%
50 PRINT "MULTIPLICATION:";A%;"*";B%;"=";A%*B%
60 PRINT "INTEGER DIVISION:";A%;"/";B%;"=";INT(A%/B%)
70 PRINT "REMAINDER OR MODULO:";A%;"%";B%;"=";A%-INT(A%/B%)*B%
80 PRINT "POWER:";A%;"^";B%;"=";A%^B%

View file

@ -0,0 +1,15 @@
! RosettaCode: Integer Arithmetic
! True BASIC v6.007
! Translated from BaCon example.
PROGRAM Integer_Arithmetic
INPUT PROMPT "Enter integer A: ": a
INPUT PROMPT "Enter integer B: ": b
PRINT
PRINT a;" + ";b;" is ";a+b
PRINT a;" - ";b;" is ";a-b
PRINT a;" * ";b;" is ";a*b
PRINT a;" / ";b;" is ";INT(a/b);
PRINT "MOD(";a;", ";b;") is "; MOD(a,b)
PRINT "POW(";a;", ";b;") is ";INT(a^b)
GET KEY done
END

View file

@ -0,0 +1,8 @@
define f(a, b) {
"add: "; a + b
"sub: "; a - b
"mul: "; a * b
"div: "; a / b /* truncates toward zero */
"mod: "; a % b /* same sign as first operand */
"pow: "; a ^ b
}

View file

@ -1,4 +1,4 @@
import std.stdio, std.string, std.conv, std.typetuple;
import std.stdio, std.string, std.conv, std.meta;
void main() {
int a = -16, b = 5;
@ -8,6 +8,6 @@ void main() {
} catch (StdioException e) {}
writeln("a = ", a, ", b = ", b);
foreach (op; TypeTuple!("+", "-", "*", "/", "%", "^^"))
foreach (op; AliasSeq!("+", "-", "*", "/", "%", "^^"))
mixin(`writeln("a ` ~ op ~ ` b = ", a` ~ op ~ `b);`);
}

View file

@ -0,0 +1,10 @@
[Enter 2 integers on 1 line.
Use whitespace to separate. Example: 2 3
Use underscore for negative integers. Example: _10
]P ? sb sa
[add: ]P la lb + p sz
[sub: ]P la lb - p sz
[mul: ]P la lb * p sz
[div: ]P la lb / p sz [truncates toward zero]sz
[mod: ]P la lb % p sz [sign matches first operand]sz
[pow: ]P la lb ^ p sz

View file

@ -1,17 +1,14 @@
#define system.
#define system'math.
#define extensions.
import system'math.
import extensions.
// --- Program ---
#symbol program =
program =
[
#var a := console readLine:(Integer new).
#var b := console readLine:(Integer new).
var a := console readLineTo(Integer new).
var b := console readLineTo(Integer new).
console writeLine:a:" + ": b:" = ":(a + b).
console writeLine:a:" - ": b:" = ":(a - b).
console writeLine:a:" * ": b:" = ":(a * b).
console writeLine:a:" / ": b:" = ":(a / b). // truncates towards 0
console writeLine:a:" % ":b:" = ":(a mod:b). // matches sign of first operand
console printLine(a," + ",b," = ",a + b).
console printLine(a," - ",b," = ",a - b).
console printLine(a," * ",b," = ",a * b).
console printLine(a," / ",b," = ",a / b). // truncates towards 0
console printLine(a," % ",b," = ",a mod:b). // matches sign of first operand
].

View file

@ -1,15 +1,25 @@
# Function to remove line breaks and convert string to int
get_int = fn msg -> IO.gets(msg) |> String.strip |> String.to_integer end
defmodule Arithmetic_Integer do
# Function to remove line breaks and convert string to int
defp get_int(msg) do
IO.gets(msg) |> String.strip |> String.to_integer
end
# Get user input
a = get_int.("Enter your first integer: ")
b = get_int.("Enter your second integer: ")
def task do
# Get user input
a = get_int("Enter your first integer: ")
b = get_int("Enter your second integer: ")
IO.puts "Elixir Integer Arithmetic:\n"
IO.puts "Sum: #{a + b}"
IO.puts "Difference: #{a - b}"
IO.puts "Product: #{a * b}"
IO.puts "True Division: #{a / b}" # Float
IO.puts "Division: #{div(a,b)}" # Truncated Towards 0
IO.puts "Remainder: #{rem(a,b)}" # Sign from first digit
IO.puts "Exponent: #{:math.pow(a,b)}" # Float, using Erlang's :math
IO.puts "Elixir Integer Arithmetic:\n"
IO.puts "Sum: #{a + b}"
IO.puts "Difference: #{a - b}"
IO.puts "Product: #{a * b}"
IO.puts "True Division: #{a / b}" # Float
IO.puts "Division: #{div(a,b)}" # Truncated Towards 0
IO.puts "Floor Division: #{Integer.floor_div(a,b)}" # floored integer division
IO.puts "Remainder: #{rem(a,b)}" # Sign from first digit
IO.puts "Modulo: #{Integer.mod(a,b)}" # modulo remainder (uses floored division)
IO.puts "Exponent: #{:math.pow(a,b)}" # Float, using Erlang's :math
end
end
Arithmetic_Integer.task

View file

@ -1,17 +0,0 @@
USING: combinators io kernel math math.functions math.order
math.parser prettyprint ;
"a=" "b=" [ write readln string>number ] bi@
{
[ + "sum: " write . ]
[ - "difference: " write . ]
[ * "product: " write . ]
[ / "quotient: " write . ]
[ /i "integer quotient: " write . ]
[ rem "remainder: " write . ]
[ mod "modulo: " write . ]
[ max "maximum: " write . ]
[ min "minimum: " write . ]
[ gcd "gcd: " write . drop ]
[ lcm "lcm: " write . ]
} 2cleave

View file

@ -1,13 +0,0 @@
a=8
b=12
sum: 20
difference: -4
product: 96
quotient: 2/3
integer quotient: 0
remainder: 8
modulo: 8
maximum: 12
minimum: 8
gcd: 4
lcm: 24

View file

@ -0,0 +1,8 @@
read a
read b
echo 'a + b =' (math "$a + $b") # Sum
echo 'a - b =' (math "$a - $b") # Difference
echo 'a * b =' (math "$a * $b") # Product
echo 'a / b =' (math "$a / $b") # Integer quotient
echo 'a % b =' (math "$a % $b") # Remainder
echo 'a ^ b =' (math "$a ^ $b") # Exponentation

View file

@ -1,15 +1,16 @@
import java.util.Scanner;
public class Int{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b;//integer addition is discouraged in print statements due to confusion with String concatenation
System.out.println("a + b = " + sum);
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("quotient of a / b = " + (a / b)); // truncates towards 0
System.out.println("remainder of a / b = " + (a % b)); // same sign as first operand
}
public class IntegerArithmetic {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int sum = a + b; // integer addition is discouraged in print statements due to confusion with string concatenation
System.out.println("a + b = " + sum);
System.out.println("a - b = " + (a - b));
System.out.println("a * b = " + (a * b));
System.out.println("quotient of a / b = " + (a / b)); // truncates towards 0
System.out.println("remainder of a / b = " + (a % b)); // same sign as first operand
}
}

View file

@ -1,4 +1,4 @@
function arithmetic (a = int(readline()), b = int(readline()))
function arithmetic (a = parse(Int, readline()), b = parse(Int, readline()))
for op in [+,-,*,div,rem]
println("a $op b = $(op(a,b))")
end

View file

@ -0,0 +1,41 @@
// version 1.1
fun main(args: Array<String>) {
val r = Regex("""-?\d+[ ]+-?\d+""")
while(true) {
print("Enter two integers separated by space(s) or q to quit: ")
val input: String = readLine()!!.trim()
if (input == "q" || input == "Q") break
if (!input.matches(r)) {
println("Invalid input, try again")
continue
}
val index = input.lastIndexOf(' ')
val a = input.substring(0, index).trimEnd().toLong()
val b = input.substring(index + 1).toLong()
println("$a + $b = ${a + b}")
println("$a - $b = ${a - b}")
println("$a * $b = ${a * b}")
if (b != 0L) {
println("$a / $b = ${a / b}") // rounds towards zero
println("$a % $b = ${a % b}") // if non-zero, matches sign of first operand
}
else {
println("$a / $b = undefined")
println("$a % $b = undefined")
}
val d = Math.pow(a.toDouble(), b.toDouble())
print("$a ^ $b = ")
if (d % 1.0 == 0.0) {
if (d >= Long.MIN_VALUE.toDouble() && d <= Long.MAX_VALUE.toDouble())
println("${d.toLong()}")
else
println("out of range")
}
else if (!d.isFinite())
println("not finite")
else
println("not integral")
println()
}
}

View file

@ -1,7 +1,7 @@
import parseopt,strutils
import parseopt, strutils
var
opt: TOptParser = initOptParser()
opt: OptParser = initOptParser()
str = opt.cmdLineRest.split
a: int = 0
b: int = 0
@ -9,14 +9,14 @@ var
try:
a = parseInt(str[0])
b = parseInt(str[1])
except EinvalidValue:
except ValueError:
quit("Invalid params. Two integers are expected.")
echo ("a : " & $a)
echo ("b : " & $b)
echo ("a + b : " & $(a+b))
echo ("a - b : " & $(a-b))
echo ("a * b : " & $(a*b))
echo ("a div b: " & $(a div b))
echo ("a mod b: " & $(a mod b))
echo("a : " & $a)
echo("b : " & $b)
echo("a + b : " & $(a+b))
echo("a - b : " & $(a-b))
echo("a * b : " & $(a*b))
echo("a div b: " & $(a div b))
echo("a mod b: " & $(a mod b))

View file

@ -1,22 +0,0 @@
import parseopt,strutils
var
opt: TOptParser = initOptParser()
str = opt.cmdLineRest.split
a: int = 0
b: int = 0
try:
a = parseInt(str[0])
b = parseInt(str[1])
except EinvalidValue:
quit("Invalid params. Two integers are expected.")
echo ("a : " & $a)
echo ("b : " & $b)
echo ("a + b : " & $(a+b))
echo ("a - b : " & $(a-b))
echo ("a * b : " & $(a*b))
echo ("a div b: " & $(a div b))
echo ("a mod b: " & $(a mod b))

View file

@ -0,0 +1,14 @@
(define a 8)
(define b 12)
(print "(+ " a " " b ") => " (+ a b))
(print "(- " a " " b ") => " (- a b))
(print "(* " a " " b ") => " (* a b))
(print "(/ " a " " b ") => " (/ a b))
(print "(quotient " a " " b ") => " (quot a b)) ; same as (quotient a b)
(print "(remainder " a " " b ") => " (rem a b)) ; same as (remainder a b)
(print "(modulo " a " " b ") => " (mod a b)) ; same as (modulo a b)
(import (owl math-extra))
(print "(expt " a " " b ") => " (expt a b))

View file

@ -0,0 +1,5 @@
; you can use more than two arguments for +,-,*,/ functions
(print (+ 1 3 5 7 9)) ; ==> 25
(print (- 1 3 5 7 9)) ; ==> -23
(print (* 1 3 5 7 9)) ; ==> 945 - same as (1*3*5*7*9)
(print (/ 1 3 5 7 9)) ; ==> 1/945 - same as (((1/3)/5)/7)/9

View file

@ -0,0 +1,20 @@
-- test.sql
-- Tested in SQL*plus
drop table test;
create table test (a integer, b integer);
insert into test values ('&&A','&&B');
commit;
select a-b difference from test;
select a*b product from test;
select trunc(a/b) integer_quotient from test;
select mod(a,b) remainder from test;
select power(a,b) exponentiation from test;

View file

@ -0,0 +1,12 @@
INPUT "Enter first number.":first
INPUT "Enter second number.":second
PRINT "The sum of";first;"and";second;"is ";first+second&"."
PRINT "The difference between";first;"and";second;"is ";ABS(first-second)&"."
PRINT "The product of";first;"and";second;"is ";first*second&"."
IF second THEN
PRINT "The integer quotient of";first;"and";second;"is ";INTEG(first/second)&"."
ELSE
PRINT "Division by zero not cool."
ENDIF
PRINT "The remainder being...";first%second&"."
PRINT STR$(first);"raised to the power of";second;"is ";first^second&"."

View file

@ -0,0 +1,9 @@
let a = 6
let b = 4
print("sum =\(a+b)")
print("difference = \(a-b)")
print("product = \(a*b)")
print("Integer quotient = \(a/b)")
print("Remainder = (a%b)")
print("No operator for Exponential")

View file

@ -0,0 +1,22 @@
arithm: mov cx, a
mov bx, b
xor dx, dx
mov ax, cx
add ax, bx
mov sum, ax
mov ax, cx
imul bx
mov product, ax
mov ax, cx
sub ax, bx
mov difference, ax
mov ax, cx
idiv bx
mov quotient, ax
mov remainder, dx
ret

View file

@ -0,0 +1,7 @@
x,y:=ask("Two ints: ").split(" ").apply("toInt");
println("x+y = ",x + y);
println("x-y = ",x - y);
println("x*y = ",x * y);
println("x/y = ",x / y); // rounds toward zero
println("x%y = ",x % y); // remainder; matches sign of first operand when operands' signs differ
println("x.divr(y) = ",x.divr(y)); // (x/y,remainder); sign as above