task_url
stringlengths
30
116
task_name
stringlengths
2
86
task_description
stringlengths
0
14.4k
language_url
stringlengths
2
53
language_name
stringlengths
1
52
code
stringlengths
0
61.9k
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#FutureBasic
FutureBasic
  window 1, @"Integer Arithmetic", ( 0, 0, 400, 300 )   NSInteger a = 25 NSInteger b = 53   print "addition "a" + "b" = " (a + b) print "subtraction "a" - "b" = " (a - b) print "multiplication "a" * "b" = " (a * b) print "division "a" / "b" = " (a / b) printf @"float division  %ld / %ld = %f", a, b, (float)a / (float)b print "modulo "a" % "b" = " (a mod b) print "power "a" ^ "b" = " (a ^ b)   HandleEvents  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Gambas
Gambas
Public Sub Main() Dim a, b As String Dim c, d As Integer   Print "Enter two integer numbers, separated by space:" Input a, b   c = CInt(a) d = CInt(b)   Print "Sum: " & (c + d) Print "Difference:" & (c - d) Print "Product: " & (c * d) Print "Integer: " & (c Div d) Print "Remainder: " & (c Mod d) Print "Exponentiation: " & (c ^ d)   End  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#GAP
GAP
run := function() local a, b, f; f := InputTextUser(); Print("a =\n"); a := Int(Chomp(ReadLine(f))); Print("b =\n"); b := Int(Chomp(ReadLine(f))); Display(Concatenation(String(a), " + ", String(b), " = ", String(a + b))); Display(Concatenation(String(a), " - ", String(b), " = ", String(a - b))); Display(Concatenation(String(a), " * ", String(b), " = ", String(a * b))); Display(Concatenation(String(a), " / ", String(b), " = ", String(QuoInt(a, b)))); # toward 0 Display(Concatenation(String(a), " mod ", String(b), " = ", String(RemInt(a, b)))); # nonnegative Display(Concatenation(String(a), " ^ ", String(b), " = ", String(a ^ b))); CloseStream(f); end;
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Genie
Genie
[indent=4] /* Arithmethic/Integer, in Genie valac arithmethic-integer.gs */   init:int a:int = 0 b:int = 0 if args.length > 2 do b = int.parse(args[2]) if args.length > 1 do a = int.parse(args[1])   print @"a+b: $a plus $b is $(a+b)" print @"a-b: $a minus $b is $(a-b)" print @"a*b: $a times $b is $(a*b)" print @"a/b: $a by $b quotient is $(a/b) (rounded mode is TRUNCATION)" print @"a%b: $a by $b remainder is $(a%b) (sign matches first operand)"   print "\nGenie does not include a raise to power operator"   return 0
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#GEORGE
GEORGE
R (m) ; R (n) ; m n + P; m n - P; m n × P; m n div P; m n rem P;
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Go
Go
package main   import "fmt"   func main() { var a, b int fmt.Print("enter two integers: ") fmt.Scanln(&a, &b) fmt.Printf("%d + %d = %d\n", a, b, a+b) fmt.Printf("%d - %d = %d\n", a, b, a-b) fmt.Printf("%d * %d = %d\n", a, b, a*b) fmt.Printf("%d / %d = %d\n", a, b, a/b) // truncates towards 0 fmt.Printf("%d %% %d = %d\n", a, b, a%b) // same sign as first operand // no exponentiation operator }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Groovy
Groovy
def arithmetic = { a, b -> println """ a + b = ${a} + ${b} = ${a + b} a - b = ${a} - ${b} = ${a - b} a * b = ${a} * ${b} = ${a * b} a / b = ${a} / ${b} = ${a / b}  !!! Converts to floating point! (int)(a / b) = (int)(${a} / ${b}) = ${(int)(a / b)}  !!! Truncates downward after the fact a.intdiv(b) = ${a}.intdiv(${b}) = ${a.intdiv(b)}  !!! Behaves as if truncating downward, actual implementation varies a % b = ${a} % ${b} = ${a % b}   Exponentiation is also a base arithmetic operation in Groovy, so: a ** b = ${a} ** ${b} = ${a ** b} """ }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Harbour
Harbour
procedure Test( a, b ) ? "a+b", a + b ? "a-b", a - b ? "a*b", a * b // The quotient isn't integer, so we use the Int() function, which truncates it downward. ? "a/b", Int( a / b ) // Remainder: ? "a%b", a % b // Exponentiation is also a base arithmetic operation ? "a**b", a ** b return
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Haskell
Haskell
main = do a <- readLn :: IO Integer b <- readLn :: IO Integer putStrLn $ "a + b = " ++ show (a + b) putStrLn $ "a - b = " ++ show (a - b) putStrLn $ "a * b = " ++ show (a * b) putStrLn $ "a to the power of b = " ++ show (a ** b) putStrLn $ "a to the power of b = " ++ show (a ^ b) putStrLn $ "a to the power of b = " ++ show (a ^^ b) putStrLn $ "a `div` b = " ++ show (a `div` b) -- truncates towards negative infinity putStrLn $ "a `mod` b = " ++ show (a `mod` b) -- same sign as second operand putStrLn $ "a `divMod` b = " ++ show (a `divMod` b) putStrLn $ "a `quot` b = " ++ show (a `quot` b) -- truncates towards 0 putStrLn $ "a `rem` b = " ++ show (a `rem` b) -- same sign as first operand putStrLn $ "a `quotRem` b = " ++ show (a `quotRem` b)
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Haxe
Haxe
class BasicIntegerArithmetic { public static function main() { var args =Sys.args(); if (args.length < 2) return; var a = Std.parseFloat(args[0]); var b = Std.parseFloat(args[1]); trace("a+b = " + (a+b)); trace("a-b = " + (a-b)); trace("a*b = " + (a*b)); trace("a/b = " + (a/b)); trace("a%b = " + (a%b)); } }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#HicEst
HicEst
DLG(Edit=A, Edit=B, TItle='Enter numeric A and B') WRITE(Name) A, B WRITE() ' A + B = ', A + B WRITE() ' A - B = ', A - B WRITE() ' A * B = ', A * B WRITE() ' A / B = ', A / B ! no truncation WRITE() 'truncate A / B = ', INT(A / B) ! truncates towards 0 WRITE() 'round next A / B = ', NINT(A / B) ! truncates towards next integer WRITE() 'round down A / B = ', FLOOR(A / B) ! truncates towards minus infinity WRITE() 'round up A / B = ', CEILING(A / B) ! truncates towards plus infinity WRITE() 'remainder of A / B = ', MOD(A, B) ! same sign as A WRITE() 'A to the power of B = ', A ^ B WRITE() 'A to the power of B = ', A ** B
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#HolyC
HolyC
I64 *a, *b; a = Str2I64(GetStr("Enter your first number: ")); b = Str2I64(GetStr("Enter your second number: "));   if (b == 0) Print("Error: The second number must not be zero.\n"); else { Print("a + b = %d\n", a + b); Print("a - b = %d\n", a - b); Print("a * b = %d\n", a * b); Print("a / b = %d\n", a / b); /* rounds down */ Print("a % b = %d\n", a % b); /* same sign as first operand */ Print("a ` b = %d\n", a ` b); }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#i
i
main a $= integer(in(' ')); ignore b $= integer(in('\n')); ignore   print("Sum:" , a + b) print("Difference:", a - b) print("Product:" , a * b) print("Quotient:" , a / b) // rounds towards zero print("Modulus:" , a % b) // same sign as first operand print("Exponent:" , a ^ b) }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Icon_and_Unicon
Icon and Unicon
procedure main() writes("Input 1st integer a := ") a := integer(read()) writes("Input 2nd integer b := ") b := integer(read())   write(" a + b = ",a+b) write(" a - b = ",a-b) write(" a * b = ",a*b) write(" a / b = ",a/b, " rounds toward 0") write(" a % b = ",a%b, " remainder sign matches a") write(" a ^ b = ",a^b) end
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Ada
Ada
with Ada.Text_IO; use Ada.Text_IO; with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;   procedure Main is procedure divisor_count_and_sum (n : Positive; divisor_count : out Natural; divisor_sum : out Natural) is I : Positive := 1; J : Natural; begin divisor_count := 0; divisor_sum  := 0; loop J := n / I; exit when J < I; if I * J = n then divisor_sum  := divisor_sum + I; divisor_count := divisor_count + 1; if I /= J then divisor_sum  := divisor_sum + J; divisor_count := divisor_count + 1; end if; end if; I := I + 1; end loop; end divisor_count_and_sum;   arithmetic_count : Natural  := 0; composite_count  : Natural  := 0; div_count  : Natural; div_sum  : Natural; mean  : Natural; n  : Positive := 1; begin   while arithmetic_count <= 1_000_000 loop divisor_count_and_sum (n, div_count, div_sum); mean := div_sum / div_count; if mean * div_count = div_sum then arithmetic_count := arithmetic_count + 1; if div_count > 2 then composite_count := composite_count + 1; end if; if arithmetic_count <= 100 then Put (Item => n, Width => 4); if arithmetic_count mod 10 = 0 then New_Line; end if; end if; if arithmetic_count = 1_000 or else arithmetic_count = 10_000 or else arithmetic_count = 100_000 or else arithmetic_count = 1_000_000 then New_Line; Put (Item => arithmetic_count, Width => 1); Put_Line ("th arithmetic number is" & n'Image); Put_Line ("Number of composite arithmetic numbers <=" & n'Image & ":" & composite_count'Image); end if; end if; n := n + 1; end loop; end Main;
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Inform_7
Inform 7
Enter Two Numbers is a room.   Numerically entering is an action applying to one number. Understand "[number]" as numerically entering.   The first number is a number that varies.   After numerically entering for the first time: now the first number is the number understood.   After numerically entering for the second time: let A be the first number; let B be the number understood; say "[A] + [B] = [A + B]."; [operator syntax] say "[A] - [B] = [A minus B]."; [English syntax] let P be given by P = A * B where P is a number; [inline equation] say "[A] * [B] = [P]."; let Q be given by the Division Formula; [named equation] say "[A] / [B] = [Q]."; say "[A] mod [B] = [remainder after dividing A by B]."; end the story.   Equation - Division Formula Q = A / B where Q is a number, A is a number, and B is a number.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#J
J
calc =: + , - , * , <.@% , |~ , ^
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#ALGOL_68
ALGOL 68
BEGIN # find arithmetic numbers - numbers whose average divisor is an integer # # i.e. sum of divisors MOD count of divisors = 0 # INT max number = 500 000; # maximum number we will consider # [ 1 : max number ]INT d sum; [ 1 : max number ]INT d count; # all positive integers are divisible by 1 and so have at least 1 divisor # FOR i TO max number DO d sum[ i ] := d count[ i ] := 1 OD; # construct the divisor sums and counts # FOR i FROM 2 TO max number OVER 2 DO FOR j FROM i BY i TO max number DO d count[ j ] +:= 1; d sum[ j ] +:= i OD OD; # count arithmetic numbers, and show the first 100, the 1 000th, 10 000th # # and the 100 000th and show how many are composite # INT max arithmetic = 100 000; INT a count := 0; INT c count := 0; FOR i TO max number WHILE a count < max arithmetic DO IF d sum[ i ] MOD d count[ i ] = 0 THEN # have an arithmetic number # IF d count[ i ] > 2 THEN # the number is composite # c count +:= 1 FI; a count +:= 1; IF a count <= 100 THEN print( ( " ", whole( i, -3 ) ) ); IF a count MOD 10 = 0 THEN print( ( newline ) ) FI ELIF a count = 1 000 OR a count = 10 000 OR a count = 100 000 THEN print( ( newline ) ); print( ( "The ", whole( a count, 0 ) , "th arithmetic number is: ", whole( i, 0 ) , newline ) ); print( ( " There are ", whole( c count, 0 ) , " composite arithmetic numbers up to ", whole( i, 0 ) , newline ) ) FI FI OD END
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Java
Java
import java.util.Scanner;   public class IntegerArithmetic { public static void main(String[] args) { // Get the 2 numbers from command line arguments Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt();   int sum = a + b; // The result of adding 'a' and 'b' (Note: integer addition is discouraged in print statements due to confusion with string concatenation) int difference = a - b; // The result of subtracting 'b' from 'a' int product = a * b; // The result of multiplying 'a' and 'b' int division = a / b; // The result of dividing 'a' by 'b' (Note: 'division' does not contain the fractional result) int remainder = a % b; // The remainder of dividing 'a' by 'b'   System.out.println("a + b = " + sum); System.out.println("a - b = " + difference); System.out.println("a * b = " + product); System.out.println("quotient of a / b = " + division); // truncates towards 0 System.out.println("remainder of a / b = " + remainder); // same sign as first operand } }
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#BASIC
BASIC
LET n = 1 DO LET div = 1 LET divcnt = 0 LET sum = 0 DO LET quot = n/div IF quot < div THEN EXIT DO IF REMAINDER(n, div) = 0 THEN IF quot = div THEN  !n IS a square LET sum = sum+quot LET divcnt = divcnt+1 EXIT DO ELSE LET sum = sum+div+quot LET divcnt = divcnt+2 END IF END IF LET div = div+1 LOOP   IF REMAINDER(sum, divcnt) = 0 THEN  !n IS arithmetic LET arithcnt = arithcnt+1 IF arithcnt <= 100 THEN PRINT USING "####": n; IF REMAINDER(arithcnt, 20) = 0 THEN PRINT END IF IF divcnt > 2 THEN LET compcnt = compcnt+1 SELECT CASE arithcnt CASE 1000 PRINT PRINT USING "The #######th arithmetic number is #####,### up to which ###,### are composite.": arithcnt, n, compcnt CASE 10000, 100000, 1000000 PRINT USING "The #######th arithmetic number is #####,### up to which ###,### are composite.": arithcnt, n, compcnt CASE ELSE REM END SELECT END IF LET n = n+1 LOOP UNTIL arithcnt >= 1000000 END
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#JavaScript
JavaScript
var a = parseInt(get_input("Enter an integer"), 10); var b = parseInt(get_input("Enter an integer"), 10);   WScript.Echo("a = " + a); WScript.Echo("b = " + b); WScript.Echo("sum: a + b = " + (a + b)); WScript.Echo("difference: a - b = " + (a - b)); WScript.Echo("product: a * b = " + (a * b)); WScript.Echo("quotient: a / b = " + (a / b | 0)); // "| 0" casts it to an integer WScript.Echo("remainder: a % b = " + (a % b));   function get_input(prompt) { output(prompt); try { return WScript.StdIn.readLine(); } catch(e) { return readline(); } } function output(prompt) { try { WScript.Echo(prompt); } catch(e) { print(prompt); } }
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#C
C
#include <stdio.h>   void divisor_count_and_sum(unsigned int n, unsigned int* pcount, unsigned int* psum) { unsigned int divisor_count = 1; unsigned int divisor_sum = 1; unsigned int power = 2; for (; (n & 1) == 0; power <<= 1, n >>= 1) { ++divisor_count; divisor_sum += power; } for (unsigned int p = 3; p * p <= n; p += 2) { unsigned int count = 1, sum = 1; for (power = p; n % p == 0; power *= p, n /= p) { ++count; sum += power; } divisor_count *= count; divisor_sum *= sum; } if (n > 1) { divisor_count *= 2; divisor_sum *= n + 1; } *pcount = divisor_count; *psum = divisor_sum; }   int main() { unsigned int arithmetic_count = 0; unsigned int composite_count = 0;   for (unsigned int n = 1; arithmetic_count <= 1000000; ++n) { unsigned int divisor_count; unsigned int divisor_sum; divisor_count_and_sum(n, &divisor_count, &divisor_sum); if (divisor_sum % divisor_count != 0) continue; ++arithmetic_count; if (divisor_count > 2) ++composite_count; if (arithmetic_count <= 100) { printf("%3u ", n); if (arithmetic_count % 10 == 0) printf("\n"); } if (arithmetic_count == 1000 || arithmetic_count == 10000 || arithmetic_count == 100000 || arithmetic_count == 1000000) { printf("\n%uth arithmetic number is %u\n", arithmetic_count, n); printf("Number of composite arithmetic numbers <= %u: %u\n", n, composite_count); } } return 0; }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#jq
jq
# Lines which do not have two integers are skipped:   def arithmetic: split(" ") | select(length > 0) | map(tonumber) | if length > 1 then .[0] as $a | .[1] as $b | "For a = \($a) and b = \($b):\n" + "a + b = \($a + $b)\n" + "a - b = \($a - $b)\n" + "a * b = \($a * $b)\n" + "a/b|floor = \($a / $b | floor)\n" + "a % b = \($a % $b)\n" + "a | exp = \($a | exp)\n" else empty end ;   arithmetic  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Jsish
Jsish
"use strict"; /* Arthimetic/Integer, in Jsish */ var line = console.input(); var nums = line.match(/^\s*([+-]?[0-9]+)\s+([+-]?[0-9]+)\s*/); var a = Number(nums[1]); var b = Number(nums[2]);   puts("A is ", a, ", B is ", b); puts("Sum A + B is ", a + b); puts("Difference A - B is ", a - b); puts("Product A * B is ", a * b); puts("Integer quotient A / B is ", a / b | 0, " truncates toward 0"); puts("Remainder A % B is ", a % b, " sign follows first operand"); puts("Exponentiation A to the power B is ", Math.pow(a, b));   /* =!INPUTSTART!= 7 4 =!INPUTEND!= */     /* =!EXPECTSTART!= A is 7 , B is 4 Sum A + B is 11 Difference A - B is 3 Product A * B is 28 Integer quotient A / B is 1 truncates toward 0 Remainder A % B is 3 sign follows first operand Exponentiation A to the power B is 2401 =!EXPECTEND!= */
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#C.2B.2B
C++
#include <cstdio>   void divisor_count_and_sum(unsigned int n, unsigned int& divisor_count, unsigned int& divisor_sum) { divisor_count = 0; divisor_sum = 0; for (unsigned int i = 1;; i++) { unsigned int j = n / i; if (j < i) break; if (i * j != n) continue; divisor_sum += i; divisor_count += 1; if (i != j) { divisor_sum += j; divisor_count += 1; } } }   int main() { unsigned int arithmetic_count = 0; unsigned int composite_count = 0;   for (unsigned int n = 1; arithmetic_count <= 1000000; n++) { unsigned int divisor_count; unsigned int divisor_sum; divisor_count_and_sum(n, divisor_count, divisor_sum); unsigned int mean = divisor_sum / divisor_count; if (mean * divisor_count != divisor_sum) continue; arithmetic_count++; if (divisor_count > 2) composite_count++; if (arithmetic_count <= 100) { // would prefer to use <stream> and <format> in C++20 std::printf("%3u ", n); if (arithmetic_count % 10 == 0) std::printf("\n"); } if ((arithmetic_count == 1000) || (arithmetic_count == 10000) || (arithmetic_count == 100000) || (arithmetic_count == 1000000)) { std::printf("\n%uth arithmetic number is %u\n", arithmetic_count, n); std::printf("Number of composite arithmetic numbers <= %u: %u\n", n, composite_count); } } return 0; }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Julia
Julia
function arithmetic (a = parse(Int, readline()), b = parse(Int, readline())) for op in [+,-,*,div,rem] println("a $op b = $(op(a,b))") end end
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Kotlin
Kotlin
// 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() } }
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Factor
Factor
USING: combinators formatting grouping io kernel lists lists.lazy math math.functions math.primes math.primes.factors math.statistics math.text.english prettyprint sequences tools.memory.private ;   : arith? ( n -- ? ) divisors mean integer? ; : larith ( -- list ) 1 lfrom [ arith? ] lfilter ; : arith ( m -- seq ) larith ltake list>array ; : composite? ( n -- ? ) dup 1 > swap prime? not and ; : ordinal ( n -- str ) [ commas ] keep ordinal-suffix append ;   : info. ( n -- ) { [ ordinal "%s arithmetic number: " printf ] [ arith dup last commas print ] [ commas "Number of composite arithmetic numbers <= %s: " printf ] [ drop [ composite? ] count commas print nl ] } cleave ;     "First 100 arithmetic numbers:" print 100 arith 10 group simple-table. nl { 3 4 5 6 } [ 10^ info. ] each
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#FreeBASIC
FreeBASIC
' Rosetta Code problem: https://rosettacode.org/wiki/Arithmetic_numbers ' by Jjuanhdez, 06/2022   Dim As Double t0 = Timer Dim As Integer N = 1, ArithCnt = 0, CompCnt = 0 Dim As Integer Div, DivCnt, Sum, Quot   Print "The first 100 arithmetic numbers are:" Do Div = 1 : DivCnt = 0 : Sum = 0 Do Quot = N / Div If Quot < Div Then Exit Do If Quot = Div AndAlso (N Mod Div) = 0 Then 'N is a square Sum += Quot DivCnt += 1 Exit Do End If If (N Mod Div) = 0 Then Sum += Div + Quot DivCnt += 2 End If Div += 1 Loop   If (Sum Mod DivCnt) = 0 Then 'N is arithmetic ArithCnt += 1 If ArithCnt <= 100 Then Print Using "####"; N; If (ArithCnt Mod 20) = 0 Then Print End If If DivCnt > 2 Then CompCnt += 1 Select Case ArithCnt Case 1e3 Print Using !"\nThe #######th arithmetic number is #####,### up to which ###,### are composite."; ArithCnt; N; CompCnt Case 1e4, 1e5, 1e6 Print Using "The #######th arithmetic number is #####,### up to which ###,### are composite."; ArithCnt; N; CompCnt End Select End If N += 1 Loop Until ArithCnt >= 1e6 Print !"\nTook"; Timer - t0; " seconds on i5 @3.20 GHz" Sleep
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#LabVIEW
LabVIEW
  {def arithmetic {lambda {:x :y} {S.map {{lambda {:x :y :op} {br}applying :op on :x & :y returns {:op :x :y}} :x :y} + - * / % pow max min = > <}}} -> arithmetic   {arithmetic 8 12} -> applying + on 8 & 12 returns 20 applying - on 8 & 12 returns -4 applying * on 8 & 12 returns 96 applying / on 8 & 12 returns 0.6666666666666666 applying % on 8 & 12 returns 8 applying pow on 8 & 12 returns 68719476736 applying max on 8 & 12 returns 12 applying min on 8 & 12 returns 8 applying = on 8 & 12 returns false applying > on 8 & 12 returns false applying < on 8 & 12 returns true  
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Delphi
Delphi
  {{works with| Delphi-6 or better}} program ArithmeiticNumbers;   {$APPTYPE CONSOLE}   procedure ArithmeticNumbers; var N, ArithCnt, CompCnt, DDiv: integer; var DivCnt, Sum, Quot, Rem: integer; begin N:= 1; ArithCnt:= 0; CompCnt:= 0; repeat begin DDiv:= 1; DivCnt:= 0; Sum:= 0; while true do begin Quot:= N div DDiv; Rem:=N mod DDiv; if Quot < DDiv then break; if (Quot = DDiv) and (Rem = 0) then //N is a square begin Sum:= Sum+Quot; DivCnt:= DivCnt+1; break; end; if Rem = 0 then begin Sum:= Sum + DDiv + Quot; DivCnt:= DivCnt+2; end; DDiv:= DDiv+1; end; if (Sum mod DivCnt) = 0 then //N is arithmetic begin ArithCnt:= ArithCnt+1; if ArithCnt <= 100 then begin Write(N:4); if (ArithCnt mod 20) = 0 then WriteLn; end; if DivCnt > 2 then CompCnt:= CompCnt+1; case ArithCnt of 1000, 10000, 100000, 1000000: begin Writeln; Write(N, #9 {tab} ); Write(CompCnt); end; end; end; N:= N+1; end until ArithCnt >= 1000000; WriteLn; end;   begin ArithmeticNumbers; WriteLn('Hit Any Key'); ReadLn; end.  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Lambdatalk
Lambdatalk
  {def arithmetic {lambda {:x :y} {S.map {{lambda {:x :y :op} {br}applying :op on :x & :y returns {:op :x :y}} :x :y} + - * / % pow max min = > <}}} -> arithmetic   {arithmetic 8 12} -> applying + on 8 & 12 returns 20 applying - on 8 & 12 returns -4 applying * on 8 & 12 returns 96 applying / on 8 & 12 returns 0.6666666666666666 applying % on 8 & 12 returns 8 applying pow on 8 & 12 returns 68719476736 applying max on 8 & 12 returns 12 applying min on 8 & 12 returns 8 applying = on 8 & 12 returns false applying > on 8 & 12 returns false applying < on 8 & 12 returns true  
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Go
Go
package main   import ( "fmt" "math" "rcu" "sort" )   func main() { arithmetic := []int{1} primes := []int{} limit := int(1e6) for n := 3; len(arithmetic) < limit; n++ { divs := rcu.Divisors(n) if len(divs) == 2 { primes = append(primes, n) arithmetic = append(arithmetic, n) } else { mean := float64(rcu.SumInts(divs)) / float64(len(divs)) if mean == math.Trunc(mean) { arithmetic = append(arithmetic, n) } } } fmt.Println("The first 100 arithmetic numbers are:") rcu.PrintTable(arithmetic[0:100], 10, 3, false)   for _, x := range []int{1e3, 1e4, 1e5, 1e6} { last := arithmetic[x-1] lastc := rcu.Commatize(last) fmt.Printf("\nThe %sth arithmetic number is: %s\n", rcu.Commatize(x), lastc) pcount := sort.SearchInts(primes, last) + 1 if !rcu.IsPrime(last) { pcount-- } comp := x - pcount - 1 // 1 is not composite compc := rcu.Commatize(comp) fmt.Printf("The count of such numbers <= %s which are composite is %s.\n", lastc, compc) } }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Lasso
Lasso
local(a = 6, b = 4) #a + #b // 10 #a - #b // 2 #a * #b // 24 #a / #b // 1 #a % #b // 2 math_pow(#a,#b) // 1296 math_pow(#b,#a) // 4096
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#LFE
LFE
  (defmodule arith (export all))   (defun demo-arith () (case (: io fread '"Please enter two integers: " '"~d~d") ((tuple 'ok (a b)) (: io format '"~p + ~p = ~p~n" (list a b (+ a b))) (: io format '"~p - ~p = ~p~n" (list a b (- a b))) (: io format '"~p * ~p = ~p~n" (list a b (* a b))) (: io format '"~p^~p = ~p~n" (list a b (: math pow a b))) ; div truncates towards zero (: io format '"~p div ~p = ~p~n" (list a b (div a b))) ; rem's result takes the same sign as the first operand (: io format '"~p rem ~p = ~p~n" (list a b (rem a b))))))  
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#J
J
factors=: {{ */@>,{(^ [:i.1+])&.>/__ q:y}} isArith=: {{ (= <.) (+/%#) factors|y}}"0
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Julia
Julia
using Primes   function isarithmetic(n) f = [one(n)] for (p,e) in factor(n) f = reduce(vcat, [f*p^j for j in 1:e], init=f) end return rem(sum(f), length(f)) == 0 end   function arithmetic(n) i, arr = 1, Int[] while length(arr) < n isarithmetic(i) && push!(arr, i) i += 1 end return arr end   a1M = arithmetic(1_000_000) composites = [!isprime(i) for i in a1M]   println("The first 100 arithmetic numbers are:") foreach(p -> print(lpad(p[2], 5), p[1] % 20 == 0 ? "\n" : ""), enumerate(a1M[1:100]))   println("\n X Xth in Series Composite") for n in [1000, 10_000, 100_000, 1_000_000] println(lpad(n, 9), lpad(a1M[n], 12), lpad(sum(composites[2:n]), 14)) end  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Liberty_BASIC
Liberty BASIC
  input "Enter the first integer: "; first input "Enter the second integer: "; second   print "The sum is " ; first + second print "The difference is " ; first -second print "The product is " ; first *second if second <>0 then print "The integer quotient is " ; int( first /second); " (rounds towards 0)" else print "Division by zero not allowed." print "The remainder is " ; first MOD second; " (sign matches first operand)" print "The first raised to the power of the second is " ; first ^second  
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Pascal
Pascal
  program ArithmeiticNumbers;   procedure ArithmeticNumbers; var N, ArithCnt, CompCnt, DDiv: longint; var DivCnt, Sum, Quot, Rem: longint; begin N:= 1; ArithCnt:= 0; CompCnt:= 0; repeat begin DDiv:= 1; DivCnt:= 0; Sum:= 0; while true do begin Quot:= N div DDiv; Rem:=N mod DDiv; if Quot < DDiv then break; if (Quot = DDiv) and (Rem = 0) then //N is a square begin Sum:= Sum+Quot; DivCnt:= DivCnt+1; break; end; if Rem = 0 then begin Sum:= Sum + DDiv + Quot; DivCnt:= DivCnt+2; end; DDiv:= DDiv+1; end; if (Sum mod DivCnt) = 0 then //N is arithmetic begin ArithCnt:= ArithCnt+1; if ArithCnt <= 100 then begin Write(N:4); if (ArithCnt mod 20) = 0 then WriteLn; end; if DivCnt > 2 then CompCnt:= CompCnt+1; case ArithCnt of 1000, 10000, 100000, 1000000: begin Writeln; Write(N, #9 {tab} ); Write(CompCnt); end; end; end; N:= N+1; end until ArithCnt >= 1000000; WriteLn; end;   begin ArithmeticNumbers; WriteLn('Hit Any Key'); {$IFDEF WINDOWS}ReadLn;{$ENDIF} end.  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#LIL
LIL
# Arithmetic/Integer, in LIL write "Enter two numbers separated by space: " if {[canread]} {set line [readline]} print   set a [index $line 0] set b [index $line 1] print "A is $a"", B is $b" print "Sum A + B is [expr $a + $b]" print "Difference A - B is [expr $a - $b]" print "Product A * B is [expr $a * $b]" print "Integer Quotient A \\ B is [expr $a \ $b], truncates toward zero" print "Remainder A % B is [expr $a % $b], sign follows first operand" print "LIL has no exponentiation expression operator"
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Perl
Perl
use strict; use warnings; use feature 'say';   use List::Util <max sum>; use ntheory <is_prime divisors>; use Lingua::EN::Numbers qw(num2en num2en_ordinal);   sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r } sub table { my $t = 10 * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }   my @A = 0; for my $n (1..2E6) { my @div = divisors $n; push @A, $n if 0 == sum(@div) % @div; }   say "The first @{[num2en 100]} arithmetic numbers:"; say table @A[1..100];   for my $x (1E3, 1E4, 1E5, 1E6) { say "\nThe @{[num2en_ordinal $x]}: " . comma($A[$x]) . "\nComposite arithmetic numbers ≤ @{[comma $A[$x]]}: " . comma -1 + grep { not is_prime($_) } @A[1..$x]; }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Lingo
Lingo
-- X, Y: 2 editable field members, shown as sprites in the current GUI x = integer(member("X").text) y = integer(member("Y").text)   put "Sum: " , x + y put "Difference: ", x - y put "Product: " , x * y put "Quotient: " , x / y -- Truncated towards zero put "Remainder: " , x mod y -- Result has sign of left operand put "Exponent: " , power(x, y)
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Little
Little
# Maybe you need to import the mathematical funcions # from Tcl with: # eval("namespace path ::tcl::mathfunc");   void main() { int a, b; puts("Enter two integers:"); a = (int)(gets(stdin)); b = (int)(gets(stdin)); puts("${a} + ${b} = ${a+b}"); puts("${a} - ${b} = ${a-b}"); puts("${a} * ${b} = ${a*b}"); puts("${a} / ${b} = ${a/b}, remainder ${a%b}"); puts("${a} to the power of ${b} = ${(int)pow(a,b)}"); }
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Phix
Phix
with javascript_semantics sequence arithmetic = {1} integer composite = 0 function get_arithmetic(integer nth) integer n = arithmetic[$]+1 while length(arithmetic)<nth do sequence divs = factors(n,1) if remainder(sum(divs),length(divs))=0 then composite += length(divs)>2 arithmetic &= n end if n += 1 end while return arithmetic[nth] end function {} = get_arithmetic(100) printf(1,"The first 100 arithmetic numbers are:\n%s\n", {join_by(arithmetic,1,10," ",fmt:="%3d")}) constant fmt = "The %,dth arithmetic number is %,d up to which %,d are composite.\n" for n in {1e3,1e4,1e5,1e6} do integer nth = get_arithmetic(n) printf(1,fmt,{n,nth,composite}) end for
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Python
Python
def factors(n: int): f = set([1, n]) i = 2 while True: j = n // i if j < i: break if i * j == n: f.add(i) f.add(j) i += 1 return f   arithmetic_count = 0 composite_count = 0 n = 1 while arithmetic_count <= 1000000: f = factors(n) if (sum(f)/len(f)).is_integer(): arithmetic_count += 1 if len(f) > 2: composite_count += 1 if arithmetic_count <= 100: print(f'{n:3d} ', end='') if arithmetic_count % 10 == 0: print() if arithmetic_count in (1000, 10000, 100000, 1000000): print(f'\n{arithmetic_count}th arithmetic number is {n}') print(f'Number of composite arithmetic numbers <= {n}: {composite_count}') n += 1
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#LiveCode
LiveCode
ask "enter 2 numbers (comma separated)" if it is not empty then put item 1 of it into n1 put item 2 of it into n2 put sum(n1,n2) into ai["sum"] put n1 * n2 into ai["product"] put n1 div n2 into ai["quotient"] -- truncates put n1 mod n2 into ai["remainder"] put n1^n2 into ai["power"] combine ai using comma and colon put ai end if
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Logo
Logo
to operate :a :b (print [a =] :a) (print [b =] :b) (print [a + b =] :a + :b) (print [a - b =] :a - :b) (print [a * b =] :a * :b) (print [a / b =] int :a / :b) (print [a mod b =] modulo :a :b) end
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Raku
Raku
use Prime::Factor; use Lingua::EN::Numbers;   my @arithmetic = lazy (1..∞).hyper.grep: { my @div = .&divisors; (@div.sum / +@div).narrow ~~ Int }   say "The first { .Int.&cardinal } arithmetic numbers:\n", @arithmetic[^$_].batch(10)».fmt("%{.chars}d").join: "\n" given 1e2;   for 1e3, 1e4, 1e5, 1e6 { say "\nThe { .Int.&ordinal }: { comma @arithmetic[$_-1] }"; say "Composite arithmetic numbers ≤ { comma @arithmetic[$_-1] }: { comma +@arithmetic[^$_].grep({!.is-prime}) - 1 }"; }
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Rust
Rust
fn divisor_count_and_sum(mut n: u32) -> (u32, u32) { let mut divisor_count = 1; let mut divisor_sum = 1; let mut power = 2; while (n & 1) == 0 { divisor_count += 1; divisor_sum += power; power <<= 1; n >>= 1; } let mut p = 3; while p * p <= n { let mut count = 1; let mut sum = 1; power = p; while n % p == 0 { count += 1; sum += power; power *= p; n /= p; } divisor_count *= count; divisor_sum *= sum; p += 2; } if n > 1 { divisor_count *= 2; divisor_sum *= n + 1; } (divisor_count, divisor_sum) }   fn main() { let mut arithmetic_count = 0; let mut composite_count = 0; let mut n = 1; while arithmetic_count <= 1000000 { let (divisor_count, divisor_sum) = divisor_count_and_sum(n); if divisor_sum % divisor_count != 0 { n += 1; continue; } arithmetic_count += 1; if divisor_count > 2 { composite_count += 1; } if arithmetic_count <= 100 { print!("{:3} ", n); if arithmetic_count % 10 == 0 { println!(); } } if arithmetic_count == 1000 || arithmetic_count == 10000 || arithmetic_count == 100000 || arithmetic_count == 1000000 { println!("\n{}th arithmetic number is {}", arithmetic_count, n); println!( "Number of composite arithmetic numbers <= {}: {}", n, composite_count ); } n += 1; } }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#LSE64
LSE64
over : 2 pick 2dup : over over   arithmetic : \ " A=" ,t over , sp " B=" ,t dup , nl \ " A+B=" ,t 2dup + , nl \ " A-B=" ,t 2dup - , nl \ " A*B=" ,t 2dup * , nl \ " A/B=" ,t 2dup / , nl \ " A%B=" ,t  % , nl
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Lua
Lua
local x = io.read() local y = io.read()   print ("Sum: " , (x + y)) print ("Difference: ", (x - y)) print ("Product: " , (x * y)) print ("Quotient: " , (x / y)) -- Does not truncate print ("Remainder: " , (x % y)) -- Result has sign of right operand print ("Exponent: " , (x ^ y))
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#Wren
Wren
import "./math" for Int, Nums import "./fmt" for Fmt import "./sort" for Find   var arithmetic = [1] var primes = [] var limit = 1e6 var n = 3 while (arithmetic.count < limit) { var divs = Int.divisors(n) if (divs.count == 2) { primes.add(n) arithmetic.add(n) } else { var mean = Nums.mean(divs) if (mean.isInteger) arithmetic.add(n) } n = n + 1 } System.print("The first 100 arithmetic numbers are:") Fmt.tprint("$3d", arithmetic[0..99], 10)   for (x in [1e3, 1e4, 1e5, 1e6]) { var last = arithmetic[x-1] Fmt.print("\nThe $,dth arithmetic number is: $,d", x, last) var pcount = Find.nearest(primes, last) + 1 if (!Int.isPrime(last)) pcount = pcount - 1 var comp = x - pcount - 1 // 1 is not composite Fmt.print("The count of such numbers <= $,d which are composite is $,d.", last, comp) }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#M2000_Interpreter
M2000 Interpreter
  MODULE LikeCommodoreBasic { \\ ADDITION: EUCLIDEAN DIV# & MOD# AND ** FOR POWER INCLUDING ^ 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%;"DIV";B%;"=";A% DIV B% 65 PRINT "INTEGER EUCLIDEAN DIVISION:";A%;"DIV";B%;"=";A% DIV# B% 70 PRINT "REMAINDER OR MODULO:";A%;"MOD";B%;"=";A% MOD B% 75 PRINT "EUCLIDEAN REMAINDER OR MODULO:";A%;"MOD#";B%;"=";A% MOD# B% 80 PRINT "POWER:";A%;"^";B%;"=";A%^B% 90 PRINT "POWER:";A%;"**";B%;"=";A%**B% } LikeCommodoreBasic     Module IntegerTypes { a=12% ' Integer 16 bit b=12& ' Long 32 bit c=12@' Decimal (29 digits) Def ExpType$(x)=Type$(x) Print ExpType$(a+1)="Double" Print ExpType$(a+1%)="Integer" Print ExpType$(a div 5)="Double" Print ExpType$(a div 5%)="Double" Print ExpType$(a mod 5)="Double" Print ExpType$(a mod 5%)="Double" Print ExpType$(a**2)="Double"   Print ExpType$(b+1)="Double" Print ExpType$(b+1&)="Long" Print ExpType$(b div 5)="Double" Print ExpType$(b div 5&)="Double" Print ExpType$(b mod 5)="Double" Print ExpType$(b mod 5&)="Double" Print ExpType$(b**2)="Double"   Print ExpType$(c+1)="Decimal" Print ExpType$(c+1@)="Decimal" Print ExpType$(c div 5)="Decimal" Print ExpType$(c div 5@)="Decimal" Print ExpType$(c mod 5)="Decimal" Print ExpType$(c mod 5@)="Decimal" Print ExpType$(c**2)="Double" } IntegerTypes  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#M4
M4
eval(A+B) eval(A-B) eval(A*B) eval(A/B) eval(A%B)
http://rosettacode.org/wiki/Arithmetic_numbers
Arithmetic numbers
Definition A positive integer n is an arithmetic number if the average of its positive divisors is also an integer. Clearly all odd primes p must be arithmetic numbers because their only divisors are 1 and p whose sum is even and hence their average must be an integer. However, the prime number 2 is not an arithmetic number because the average of its divisors is 1.5. Example 30 is an arithmetic number because its 7 divisors are: [1, 2, 3, 5, 6, 10, 15, 30], their sum is 72 and average 9 which is an integer. Task Calculate and show here: 1. The first 100 arithmetic numbers. 2. The xth arithmetic number where x = 1,000 and x = 10,000. 3. How many of the first x arithmetic numbers are composite. Note that, technically, the arithmetic number 1 is neither prime nor composite. Stretch Carry out the same exercise in 2. and 3. above for x = 100,000 and x = 1,000,000. References Wikipedia: Arithmetic number OEIS:A003601 - Numbers n such that the average of the divisors of n is an integer
#XPL0
XPL0
int N, ArithCnt, CompCnt, Div, DivCnt, Sum, Quot; [Format(4, 0); N:= 1; ArithCnt:= 0; CompCnt:= 0; repeat Div:= 1; DivCnt:= 0; Sum:= 0; loop [Quot:= N/Div; if Quot < Div then quit; if Quot = Div and rem(0) = 0 then \N is a square [Sum:= Sum+Quot; DivCnt:= DivCnt+1; quit]; if rem(0) = 0 then [Sum:= Sum + Div + Quot; DivCnt:= DivCnt+2; ]; Div:= Div+1; ]; if rem(Sum/DivCnt) = 0 then \N is arithmetic [ArithCnt:= ArithCnt+1; if ArithCnt <= 100 then [RlOut(0, float(N)); if rem(ArithCnt/20) = 0 then CrLf(0); ]; if DivCnt > 2 then CompCnt:= CompCnt+1; case ArithCnt of 1000, 10_000, 100_000, 1_000_000: [CrLf(0); IntOut(0, N); ChOut(0, 9\tab\); IntOut(0, CompCnt); ] other; ]; N:= N+1; until ArithCnt >= 1_000_000; ]
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Maple
Maple
  DoIt := proc() local a := readstat( "Input an integer: " ): local b := readstat( "Input another integer: " ): printf( "Sum = %d\n", a + b ): printf( "Difference = %d\n", a - b ): printf( "Product = %d\n", a * b ): printf( "Quotient = %d\n", iquo( a, b, 'c' ) ): printf( "Remainder = %d\n", c ); # or irem( a, b ) NULL # quiet return end proc:  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Mathematica.2FWolfram_Language
Mathematica/Wolfram Language
a = Input["Give me an integer please!"]; b = Input["Give me another integer please!"]; Print["You gave me ", a, " and ", b]; Print["sum: ", a + b]; Print["difference: ", a - b]; Print["product: ", a b]; Print["integer quotient: ", Quotient[a, b]]; Print["remainder: ", Mod[a, b]]; Print["exponentiation: ", a^b];
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Mathcad
Mathcad
disp("integer a: "); a = scanf("%d", 1); disp("integer b: "); b = scanf("%d", 1); a+b a-b a*b floor(a/b) mod(a,b) a^b
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#MATLAB_.2F_Octave
MATLAB / Octave
disp("integer a: "); a = scanf("%d", 1); disp("integer b: "); b = scanf("%d", 1); a+b a-b a*b floor(a/b) mod(a,b) a^b
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Maxima
Maxima
block( [a: read("a"), b: read("b")], print(a + b), print(a - b), print(a * b), print(a / b), print(quotient(a, b)), print(remainder(a, b)), a^b );
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#MAXScript
MAXScript
x = getKBValue prompt:"First number" y = getKBValue prompt:"Second number:"   format "Sum: %\n" (x + y) format "Difference: %\n" (x - y) format "Product: %\n" (x * y) format "Quotient: %\n" (x / y) format "Remainder: %\n" (mod x y)
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Mercury
Mercury
  :- module arith_int. :- interface.   :- import_module io. :- pred main(io::di, io::uo) is det.   :- implementation. :- import_module int, list, string.   main(!IO) :- io.command_line_arguments(Args, !IO), ( if Args = [AStr, BStr], string.to_int(AStr, A), string.to_int(BStr, B) then io.format("A + B = %d\n", [i(A + B)], !IO), io.format("A - B = %d\n", [i(A - B)], !IO), io.format("A * B = %d\n", [i(A * B)], !IO),    % Division: round towards zero.  % io.format("A / B = %d\n", [i(A / B)], !IO),    % Division: round towards minus infinity.  % io.format("A div B = %d\n", [i(A div B)], !IO),    % Modulus: X mod Y = X - (X div Y) * Y.  % io.format("A mod B = %d\n", [i(A mod B)], !IO),    % Remainder: X rem Y = X - (X / Y) * Y.  % io.format("A rem B = %d\n", [i(A rem B)], !IO),    % Exponentiation is done using the function int.pow/2.  % io.format("A `pow` B = %d\n", [i(A `pow` B)], !IO) else io.set_exit_status(1, !IO) ).  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Metafont
Metafont
string s[]; message "input number a: "; s1 := readstring; message "input number b: "; s2 := readstring; a := scantokens s1; b := scantokens s2;   def outp(expr op) = message "a " & op & " b = " & decimal(a scantokens(op) b) enddef;   outp("+"); outp("-"); outp("*"); outp("div"); outp("mod");   end
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#min
min
(concat dup -> ' prepend "$1 -> $2" swap % puts!) :show   ("Enter an integer" ask int) 2 times ' prepend ('+ '- '* 'div 'mod) quote-map ('show concat) map cleave
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#.D0.9C.D0.9A-61.2F52
МК-61/52
П1 <-> П0 + С/П ИП0 ИП1 - С/П ИП0 ИП1 * С/П ИП0 ИП1 / [x] С/П ИП0 ^ ИП1 / [x] ИП1 * - С/П ИП1 ИП0 x^y С/П
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#ML.2FI
ML/I
MCSKIP "WITH" NL "" Arithmetic/Integer "" assumes macros on input stream 1, terminal on stream 2 MCSKIP MT,<> MCINS %. MCDEF SL SPACES NL AS <MCSET T1=%A1. MCSET T2=%A2. a + b = %%T1.+%T2.. a - b = %%T1.-%T2.. a * b = %%T1.*%T2.. a / b = %%T1./%T2.. a rem b = %%T1.-%%%T1./%T2..*%T2... Division is truncated to the greatest integer that does not exceed the exact result. Remainder matches the sign of the second operand, if the signs differ.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Modula-2
Modula-2
MODULE ints;   IMPORT InOut;   VAR a, b : INTEGER;   BEGIN InOut.WriteString ("Enter two integer numbers : "); InOut.WriteBf; InOut.ReadInt (a); InOut.ReadInt (b); InOut.WriteString ("a + b = "); InOut.WriteInt (a + b, 9); InOut.WriteLn; InOut.WriteString ("a - b = "); InOut.WriteInt (a - b, 9); InOut.WriteLn; InOut.WriteString ("a * b = "); InOut.WriteInt (a * b, 9); InOut.WriteLn; InOut.WriteString ("a / b = "); InOut.WriteInt (a DIV b, 9); InOut.WriteLn; InOut.WriteString ("a MOD b = "); InOut.WriteInt (a MOD b, 9); InOut.WriteLn; InOut.WriteLn; END ints.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Modula-3
Modula-3
MODULE Arith EXPORTS Main;   IMPORT IO, Fmt;   VAR a, b: INTEGER;   BEGIN a := IO.GetInt(); b := IO.GetInt(); IO.Put("a+b = " & Fmt.Int(a + b) & "\n"); IO.Put("a-b = " & Fmt.Int(a - b) & "\n"); IO.Put("a*b = " & Fmt.Int(a * b) & "\n"); IO.Put("a DIV b = " & Fmt.Int(a DIV b) & "\n"); IO.Put("a MOD b = " & Fmt.Int(a MOD b) & "\n"); END Arith.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#MUMPS
MUMPS
Arith(first,second) ; Mathematical operators Write "Plus",?12,first,"+",second,?25," = ",first+second,! Write "Minus",?12,first,"-",second,?25," = ",first-second,! Write "Multiply",?12,first,"*",second,?25," = ",first*second,! Write "Divide",?12,first,"/",second,?25," = ",first/second,! Write "Int Divide",?12,first,"\",second,?25," = ",first\second,! Write "Power",?12,first,"**",second,?25," = ",first**second,! Write "Modulo",?12,first,"#",second,?25," = ",first#second,! Write "And",?12,first,"&",second,?25," = ",first&second,! Write "Or",?12,first,"!",second,?25," = ",first!second,! Quit   Do Arith(2,3) Plus 2+3 = 5 Minus 2-3 = -1 Multiply 2*3 = 6 Divide 2/3 = .6666666666666666667 Int Divide 2\3 = 0 Power 2**3 = 8 Modulo 2#3 = 2 And 2&3 = 1 Or 2!3 = 1   Do Arith(16,0.5) Plus 16+.5 = 16.5 Minus 16-.5 = 15.5 Multiply 16*.5 = 8 Divide 16/.5 = 32 Int Divide 16\.5 = 32 Power 16**.5 = 4 Modulo 16#.5 = 0 And 16&.5 = 1 Or 16!.5 = 1   Do Arith(0,2) Plus 0+2 = 2 Minus 0-2 = -2 Multiply 0*2 = 0 Divide 0/2 = 0 Int Divide 0\2 = 0 Power 0**2 = 0 Modulo 0#2 = 0 And 0&2 = 0 Or 0!2 = 1
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Nanoquery
Nanoquery
print "Number 1: " x = int(input()) print "Number 2: " y = int(input())   println format("Sum: %d", x + y) println format("Difference: %d", x - y) println format("Product: %d", x * y) println format("Quotient: %f", x / y)   println format("Remainder: %d", x % y) println format("Power: %d", x ^ y)
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Nemerle
Nemerle
using System;   class Program { static Main(args : array[string]) : void { def a = Convert.ToInt32(args[0]); def b = Convert.ToInt32(args[1]);   Console.WriteLine("{0} + {1} = {2}", a, b, a + b); Console.WriteLine("{0} - {1} = {2}", a, b, a - b); Console.WriteLine("{0} * {1} = {2}", a, b, a * b); Console.WriteLine("{0} / {1} = {2}", a, b, a / b); // truncates towards 0 Console.WriteLine("{0} % {1} = {2}", a, b, a % b); // matches sign of first operand Console.WriteLine("{0} ** {1} = {2}", a, b, a ** b); } }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#NetRexx
NetRexx
/* NetRexx */   options replace format comments java crossref symbols binary   say "enter 2 integer values separated by blanks" parse ask a b say a "+" b "=" a + b say a "-" b "=" a - b say a "*" b "=" a * b say a "/" b "=" a % b "remaining" a // b "(sign from first operand)" say a "^" b "=" a ** b   return  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#NewLISP
NewLISP
; integer.lsp ; oofoe 2012-01-17   (define (aski msg) (print msg) (int (read-line))) (setq x (aski "Please type in an integer and press [enter]: ")) (setq y (aski "Please type in another integer  : "))   ; Note that +, -, *, / and % are all integer operations. (println) (println "Sum: " (+ x y)) (println "Difference: " (- x y)) (println "Product: " (* x y)) (println "Integer quotient (rounds to 0): " (/ x y)) (println "Remainder: " (setq r (% x y)))   (println "Remainder sign matches: " (cond ((= (sgn r) (sgn x) (sgn y)) "both") ((= (sgn r) (sgn x)) "first") ((= (sgn r) (sgn y)) "second")))   (println) (println "Exponentiation: " (pow x y))   (exit) ; NewLisp normally goes to listener after running script.  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Nial
Nial
arithmetic is OP A B{[first,last,+,-,*,quotient,mod,power] A B}
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Nim
Nim
import parseopt, strutils   var opt: OptParser = initOptParser() str = opt.cmdLineRest.split a: int = 0 b: int = 0   try: a = parseInt(str[0]) b = parseInt(str[1]) 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)) # div rounds towards zero echo("a mod b: " & $(a mod b)) # sign(a mod b)==sign(a) if sign(a)!=sign(b) echo("a ^ b  : " & $(a ^ b))  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#NSIS
NSIS
Function Arithmetic Push $0 Push $1 Push $2 StrCpy $0 21 StrCpy $1 -2   IntOp $2 $0 + $1 DetailPrint "$0 + $1 = $2" IntOp $2 $0 - $1 DetailPrint "$0 - $1 = $2" IntOp $2 $0 * $1 DetailPrint "$0 * $1 = $2" IntOp $2 $0 / $1 DetailPrint "$0 / $1 = $2" DetailPrint "Rounding is toward negative infinity" IntOp $2 $0 % $1 DetailPrint "$0 % $1 = $2" DetailPrint "Sign of remainder matches the first number"   Pop $2 Pop $1 Pop $0 FunctionEnd
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Oberon-2
Oberon-2
  MODULE Arithmetic; IMPORT In, Out; VAR x,y:INTEGER; BEGIN Out.String("Give two numbers: ");In.Int(x);In.Int(y); Out.String("x + y >");Out.Int(x + y,6);Out.Ln; Out.String("x - y >");Out.Int(x - y,6);Out.Ln; Out.String("x * y >");Out.Int(x * y,6);Out.Ln; Out.String("x / y >");Out.Int(x DIV y,6);Out.Ln; Out.String("x MOD y >");Out.Int(x MOD y,6);Out.Ln; END Arithmetic.  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Objeck
Objeck
bundle Default { class Arithmetic { function : Main(args : System.String[]) ~ Nil { DoArithmetic(); }   function : native : DoArithmetic() ~ Nil { a := IO.Console->GetInstance()->ReadString()->ToInt(); b := IO.Console->GetInstance()->ReadString()->ToInt();   IO.Console->GetInstance()->Print("a+b = ")->PrintLine(a+b); IO.Console->GetInstance()->Print("a-b = ")->PrintLine(a-b); IO.Console->GetInstance()->Print("a*b = ")->PrintLine(a*b); IO.Console->GetInstance()->Print("a/b = ")->PrintLine(a/b); } } }
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#OCaml
OCaml
let _ = let a = read_int () and b = read_int () in   Printf.printf "a + b = %d\n" (a + b); Printf.printf "a - b = %d\n" (a - b); Printf.printf "a * b = %d\n" (a * b); Printf.printf "a / b = %d\n" (a / b); (* truncates towards 0 *) Printf.printf "a mod b = %d\n" (a mod b) (* same sign as first operand *)
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Oforth
Oforth
: integers (a b -- ) "a + b =" . a b + .cr "a - b =" . a b - .cr "a * b =" . a b * .cr "a / b =" . a b / .cr "a mod b =" . a b mod .cr "a pow b =" . a b pow .cr ;
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Ol
Ol
  (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)   (print "(expt " a " " b ") => " (expt a b)) (print "(gcd " a " " b ") => " (gcd a b)) (print "(lcm " a " " b ") => " (lcm a b))   ; you can use more than two arguments for +,-,*,/ functions (print (+ 1 3 5 7 9)) (print (- 1 3 5 7 9)) (print (* 1 3 5 7 9)) ; same as (1*3*5*7*9) (print (/ 1 3 5 7 9)) ; same as (((1/3)/5)/7)/9  
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Onyx
Onyx
# Most of this long script is mere presentation. # All you really need to do is push two integers onto the stack # and then execute add, sub, mul, idiv, or pow.   $ClearScreen { # Using ANSI terminal control `\e[2J\e[1;1H' print flush } bind def   $Say { # string Say - `\n' cat print flush } bind def   $ShowPreamble { `To show how integer arithmetic in done in Onyx,' Say `we\'ll use two numbers of your choice, which' Say `we\'ll call A and B.\n' Say } bind def   $Prompt { # stack: string -- stdout exch write pop flush } def   $GetInt { # stack: name -- integer dup cvs `Enter integer ' exch cat `: ' cat Prompt stdin readline pop cvx eval def } bind def   $Template { # arithmetic_operator_name label_string Template result_string A cvs ` ' B cvs ` ' 5 ncat over cvs ` gives ' 3 ncat exch A B dn cvx eval cvs `.' 3 ncat Say } bind def   $ShowResults { $add `Addition: ' Template $sub `Subtraction: ' Template $mul `Multiplication: ' Template $idiv `Division: ' Template `Note that the result of integer division is rounded toward zero.' Say $pow `Exponentiation: ' Template `Note that the result of raising to a negative power always gives a real number.' Say } bind def   ClearScreen ShowPreamble $A GetInt $B GetInt ShowResults
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Openscad
Openscad
echo (a+b); /* Sum */ echo (a-b); /* Difference */ echo (a*b); /* Product */ echo (a/b); /* Quotient */ echo (a%b); /* Modulus */
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Oz
Oz
declare StdIn = {New class $ from Open.file Open.text end init(name:stdin)}   fun {ReadInt} {String.toInt {StdIn getS($)}} end   A = {ReadInt} B = {ReadInt} in {ForAll ["A+B = "#A+B "A-B = "#A-B "A*B = "#A*B "A/B = "#A div B %% truncates towards 0 "remainder "#A mod B %% has the same sign as A "A^B = "#{Pow A B} ] System.showInfo}
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Panda
Panda
a=3 b=7 func:_bbf__number_number_number =>f.name.<b> '(' a b ')' ' => ' f(a b) nl
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PARI.2FGP
PARI/GP
arith(a,b)={ print(a+b); print(a-b); print(a*b); print(a\b); print(a%b); print(a^b); };
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Pascal
Pascal
program arithmetic(input, output)   var a, b: integer;   begin readln(a, b); writeln('a+b = ', a+b); writeln('a-b = ', a-b); writeln('a*b = ', a*b); writeln('a/b = ', a div b, ', remainder ', a mod b); writeln('a^b = ',Power(a,b):4:2); {real power} writeln('a^b = ',IntPower(a,b):4:2); {integer power} end.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Perl
Perl
my $a = <>; my $b = <>;   print "sum: ", $a + $b, "\n", "difference: ", $a - $b, "\n", "product: ", $a * $b, "\n", "integer quotient: ", int($a / $b), "\n", "remainder: ", $a % $b, "\n", "exponent: ", $a ** $b, "\n" ;
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Phix
Phix
with javascript_semantics include pGUI.e Ihandle lab, tab, res, dlg constant fmt = """ a = %d b = %d a + b = %d a - b = %d a * b = %d a / b = %g (does not truncate) remainder(a,b) = %d (same sign as first operand) power(a,b) = %g """ function valuechanged_cb(Ihandle tab) string s = IupGetAttribute(tab,"VALUE") sequence r = scanf(s,"%d %d") if length(r)=1 then integer {a,b} = r[1] s = sprintf(fmt, {a, b, a+b, a-b, a*b, a/b, remainder(a,b), power(a,b)}) IupSetStrAttribute(res,"TITLE",s) IupRefresh(res) end if return IUP_DEFAULT end function procedure main() IupOpen() lab = IupLabel("Enter two numbers") tab = IupText("VALUECHANGED_CB", Icallback("valuechanged_cb"),"EXPAND=HORIZONTAL") res = IupLabel("(separated by a space)\n\n\n\n\n\n\n","EXPAND=BOTH") dlg = IupDialog(IupVbox({IupHbox({lab,tab},"GAP=10,NORMALIZESIZE=VERTICAL"), IupHbox({res})},"MARGIN=5x5"), `SIZE=188x112,TITLE="Arithmetic/Integer"`) IupShow(dlg) if platform()!=JS then IupMainLoop() IupClose() end if end procedure main()
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Phixmonti
Phixmonti
def printOp swap print print nl enddef   8 var a 3 var b "a = " a printOp "b = " b printOp   "a + b = " a b + printOp "a - b = " a b - printOp "a * b = " a b * printOp "int(a / b) = " a b / int printOp "a mod b = " a b mod printOp "a ^ b = " a b power printOp
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PHL
PHL
module arith;   extern printf; extern scanf;   @Integer main [ @Pointer<@Integer> a = alloc(4); @Pointer<@Integer> b = alloc(4); scanf("%i %i", a, b);   printf("a + b = %i\n", a::get + b::get); printf("a - b = %i\n", a::get - b::get); printf("a * b = %i\n", a::get * b::get); printf("a / b = %i\n", a::get / b::get); printf("a % b = %i\n", a::get % b::get); printf("a ** b = %i\n", a::get ** b::get);   return 0; ]
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PHP
PHP
<?php $a = fgets(STDIN); $b = fgets(STDIN);   echo "sum: ", $a + $b, "\n", "difference: ", $a - $b, "\n", "product: ", $a * $b, "\n", "truncating quotient: ", (int)($a / $b), "\n", "flooring quotient: ", floor($a / $b), "\n", "remainder: ", $a % $b, "\n", "power: ", $a ** $b, "\n"; // PHP 5.6+ only ?>
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PicoLisp
PicoLisp
(de math (A B) (prinl "Add " (+ A B)) (prinl "Subtract " (- A B)) (prinl "Multiply " (* A B)) (prinl "Divide " (/ A B)) # Trucates towards zero (prinl "Div/rnd " (*/ A B)) # Rounds to next integer (prinl "Modulus " (% A B)) # Sign of the first operand (prinl "Power " (** A B)) )
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Piet
Piet
command stack in(int) A duplicate AA duplicate AAA duplicate AAAA duplicate AAAAA in(int) BAAAAA duplicate BBAAAAA duplicate BBBAAAAA duplicate BBBBAAAAA duplicate BBBBBAAAAA push 9 9BBBBBAAAAA push 1 19BBBBBAAAAA roll BBBBAAAABA push 7 7BBBBAAAABA push 1 17BBBBAAAABA roll BBBAAABABA push 5 5BBBAAABABA push 1 15BBBAAABABA roll BBAABABABA push 3 3BBAABABABA push 1 13BBAABABABA roll BABABABABA add (A+B)BABABABA out(int) BABABABA sub (A-B)BABABA out(int) BABABA mult (A*B)BABA out(int) BABA divide (A/B)BA out(int) BA mod (A%B) out(int) NULL push 1 1 exit
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PL.2FI
PL/I
  get list (a, b); put skip list (a+b); put skip list (a-b); put skip list (a*b); put skip list (trunc(a/b)); /* truncates towards zero. */ put skip list (mod(a, b)); /* Remainder is always positive. */ put skip list (rem(a, b)); /* Sign can be negative. */
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Plain_English
Plain English
To run: Start up. Demonstrate integer arithmetic. Wait for the escape key. Shut down.   To demonstrate integer arithmetic: Write "Enter a number: " to the console without advancing. Read a number from the console. Write "Enter another number: " to the console without advancing. Read another number from the console. Show the arithmetic operations between the number and the other number.   To show the arithmetic operations between a number and another number: Write the number plus the other number then " is the sum." to the console. Write the number minus the other number then " is the difference." to the console. Write the number times the other number then " is the product." to the console. Show the division of the number by the other number. Raise the number to the other number. Write the number then " is the power." to the console.   To show the division of a number by another number: Privatize the number. Divide the number by the other number giving a quotient [rounding toward zero] and a remainder [with the same sign as the dividend]. Write the quotient then " is the quotient." to the console. Write the remainder then " is the remainder." to the console.
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#Pop11
Pop11
;;; Setup token reader vars itemrep; incharitem(charin) -> itemrep; ;;; read the numbers lvars a = itemrep(), b = itemrep(); ;;; Print results printf(a + b, 'a + b = %p\n'); printf(a - b, 'a - b = %p\n'); printf(a * b, 'a * b = %p\n'); printf(a div b, 'a div b = %p\n'); printf(a mod b, 'a mod b = %p\n');
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PostScript
PostScript
/arithInteger { /x exch def /y exch def x y add = x y sub = x y mul = x y idiv = x y mod = x y exp = } def
http://rosettacode.org/wiki/Arithmetic/Integer
Arithmetic/Integer
Basic Data Operation This is a basic data operation. It represents a fundamental action on a basic data type. You may see other such operations in the Basic Data Operations category, or: Integer Operations Arithmetic | Comparison Boolean Operations Bitwise | Logical String Operations Concatenation | Interpolation | Comparison | Matching Memory Operations Pointers & references | Addresses Task Get two integers from the user,   and then (for those two integers), display their:   sum   difference   product   integer quotient   remainder   exponentiation   (if the operator exists) Don't include error handling. For quotient, indicate how it rounds   (e.g. towards zero, towards negative infinity, etc.). For remainder, indicate whether its sign matches the sign of the first operand or of the second operand, if they are different.
#PowerShell
PowerShell
$a = [int] (Read-Host First Number) $b = [int] (Read-Host Second Number)   Write-Host "Sum: $($a + $b)" Write-Host "Difference: $($a - $b)" Write-Host "Product: $($a * $b)" Write-Host "Quotient: $($a / $b)" Write-Host "Quotient, round to even: $([Math]::Round($a / $b))" Write-Host "Remainder, sign follows first: $($a % $b)"
http://rosettacode.org/wiki/Arrays
Arrays
This task is about arrays. For hashes or associative arrays, please see Creating an Associative Array. For a definition and in-depth discussion of what an array is, see Array. Task Show basic array syntax in your language. Basically, create an array, assign a value to it, and retrieve an element   (if available, show both fixed-length arrays and dynamic arrays, pushing a value into it). Please discuss at Village Pump:   Arrays. Please merge code in from these obsolete tasks:   Creating an Array   Assigning Values to an Array   Retrieving an Element of an Array Related tasks   Collections   Creating an Associative Array   Two-dimensional array (runtime)
#11l
11l
[Int] array array.append(1) array.append(3) array[0] = 2 print(array[0]) // retrieve first element in an array print(array.last) // retrieve last element in an array print(array.pop()) // pop last item in an array print(array.pop(0)) // pop first item in an array   V size = 10 V myArray = [0] * size // create single-dimensional array V width = 3 V height = 4 V myArray2 = [[0] * width] * height // create array of arrays