June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
21
Task/Arithmetic-Rational/Factor/arithmetic-rational.factor
Normal file
21
Task/Arithmetic-Rational/Factor/arithmetic-rational.factor
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
USING: generalizations io kernel math math.functions
|
||||
math.primes.factors math.ranges prettyprint sequences ;
|
||||
IN: rosetta-code.arithmetic-rational
|
||||
|
||||
2/5 ! literal syntax 2/5
|
||||
2/4 ! automatically simplifies to 1/2
|
||||
5/1 ! automatically coerced to 5
|
||||
26/5 ! mixed fraction 5+1/5
|
||||
13/178 >fraction ! get the numerator and denominator 13 178
|
||||
8 recip ! get the reciprocal 1/8
|
||||
|
||||
! ratios can be any size
|
||||
12417829731289312/61237812937138912735712
|
||||
8 ndrop ! clear the stack
|
||||
! arithmetic works the same as any other number.
|
||||
|
||||
: perfect? ( n -- ? )
|
||||
divisors rest [ recip ] map-sum 1 = ;
|
||||
|
||||
"Perfect numbers <= 2^19: " print
|
||||
2 19 ^ [1,b] [ perfect? ] filter .
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
class Rational implements Comparable {
|
||||
class Rational extends Number implements Comparable {
|
||||
final BigInteger num, denom
|
||||
|
||||
static final Rational ONE = new Rational(1)
|
||||
|
|
@ -6,8 +6,9 @@ class Rational implements Comparable {
|
|||
|
||||
Rational(BigDecimal decimal) {
|
||||
this(
|
||||
decimal.scale() < 0 ? decimal.unscaledValue()*10**(-decimal.scale()) : decimal.unscaledValue(),
|
||||
decimal.scale() < 0 ? 1 : 10**(decimal.scale()))
|
||||
decimal.scale() < 0 ? decimal.unscaledValue() * 10 ** -decimal.scale() : decimal.unscaledValue(),
|
||||
decimal.scale() < 0 ? 1 : 10 ** decimal.scale()
|
||||
)
|
||||
}
|
||||
|
||||
Rational(BigInteger n, BigInteger d = 1) {
|
||||
|
|
@ -16,14 +17,14 @@ class Rational implements Comparable {
|
|||
}
|
||||
|
||||
private List reduce(BigInteger n, BigInteger d) {
|
||||
BigInteger sign = ((n < 0) != (d < 0)) ? -1 : 1
|
||||
BigInteger sign = ((n < 0) ^ (d < 0)) ? -1 : 1
|
||||
(n, d) = [n.abs(), d.abs()]
|
||||
BigInteger commonFactor = gcd(n, d)
|
||||
|
||||
[n.intdiv(commonFactor) * sign, d.intdiv(commonFactor)]
|
||||
}
|
||||
|
||||
public Rational toLeastTerms() { reduce(num, denom) as Rational }
|
||||
Rational toLeastTerms() { reduce(num, denom) as Rational }
|
||||
|
||||
private BigInteger gcd(BigInteger n, BigInteger m) {
|
||||
n == 0 ? m : { while(m%n != 0) { (n, m) = [m%n, n] }; n }()
|
||||
|
|
@ -83,15 +84,15 @@ class Rational implements Comparable {
|
|||
|
||||
Object asType(Class type) {
|
||||
switch (type) {
|
||||
case this.getClass(): return this
|
||||
case [Boolean.class,Boolean.TYPE]: return asBoolean()
|
||||
case BigDecimal.class: return toBigDecimal()
|
||||
case BigInteger.class: return toBigInteger()
|
||||
case [Double.class,Double.TYPE]: return toDouble()
|
||||
case [Float.class,Float.TYPE]: return toFloat()
|
||||
case [Integer.class,Integer.TYPE]: return toInteger()
|
||||
case [Long.class,Long.TYPE]: return toLong()
|
||||
case String.class: return toString()
|
||||
case this.class: return this
|
||||
case [Boolean, Boolean.TYPE]: return asBoolean()
|
||||
case BigDecimal: return toBigDecimal()
|
||||
case BigInteger: return toBigInteger()
|
||||
case [Double, Double.TYPE]: return toDouble()
|
||||
case [Float, Float.TYPE]: return toFloat()
|
||||
case [Integer, Integer.TYPE]: return toInteger()
|
||||
case [Long, Long.TYPE]: return toLong()
|
||||
case String: return toString()
|
||||
default: throw new ClassCastException("Cannot convert from type Rational to type " + type)
|
||||
}
|
||||
}
|
||||
|
|
@ -99,10 +100,10 @@ class Rational implements Comparable {
|
|||
boolean equals(o) { compareTo(o) == 0 }
|
||||
|
||||
int compareTo(o) {
|
||||
o instanceof Rational \
|
||||
? compareTo(o as Rational) \
|
||||
: o instanceof Number \
|
||||
? compareTo(o as Number)\
|
||||
o instanceof Rational
|
||||
? compareTo(o as Rational)
|
||||
: o instanceof Number
|
||||
? compareTo(o as Number)
|
||||
: (Double.NaN as int)
|
||||
}
|
||||
int compareTo(Rational r) { num*r.denom <=> denom*r.num }
|
||||
|
|
|
|||
13
Task/Arithmetic-Rational/Sidef/arithmetic-rational.sidef
Normal file
13
Task/Arithmetic-Rational/Sidef/arithmetic-rational.sidef
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
for n in (1 .. 2**19) {
|
||||
var frac = 0
|
||||
|
||||
n.divisors.each {|d|
|
||||
frac += 1/d
|
||||
}
|
||||
|
||||
if (frac.is_int) {
|
||||
say "Sum of reciprocal divisors of #{n} = #{frac} exactly #{
|
||||
frac == 2 ? '- perfect!' : ''
|
||||
}"
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue