exponentiation

Why Can't This Code Find Powers? (Ruby)

大憨熊 提交于 2019-12-11 07:55:09
问题 App Academy's practice test says their chosen way of finding if an input is a power of 2 is to keep dividing it by 2 on a loop and check whether the end result is 1 or 0 (after having tested for the numbers 1 and 0 as inputs), which makes sense, but why won't this way work? def try gets(num) counter = 0 go = 2 ** counter if num % go == 0 return true else counter = counter + 1 end return false end I can't figure out why this won't work, unless the counter isn't working. 回答1: There are a number

Transforming recursion into tail recursion?

妖精的绣舞 提交于 2019-12-10 19:17:53
问题 I am trying to write a predicate that recursively finds the nth power of some number [A^n = A * A^(n-1)] and uses the shortcut A^(2n) = A^n * A^n. Here is the solution so far. p(_,0,1):-!. p(A,N,R):-N mod 2=0,!,N1=N/2,p(A,N1,R1),R=R1*R1. p(A,N,R):-N1=N-1,p(A,N1,R1),R=R1*A. Now I want to make this tail recursive. I can do tail for simple cases, such as factorials and power without the shortcut (by adding an accumulator), but this one is hard. Any help is much appreciated! 回答1: It seems it is

Large multiplication output coming out negative in Ruby

青春壹個敷衍的年華 提交于 2019-12-10 17:23:45
问题 I wrote some code that is supposed to sum n^n for 1 <= n <= 1000. Here's the code: sum = 0 (1..1000).each do |n| sum += n**n puts "n = #{n}, sum = #{sum}" end For some reason, the output is coming out negative after number 28: n = 29, sum = -2015400977700573523892329442490139437391867 Any idea why this is happening? 回答1: Looks like this was a bug in 1.8.7 that was fixed by patch 358: Exponentiation in Ruby 1.8.7 Returns Wrong Answers (The result of a power computation in numeric.c wasn't

Power (exponentiation) and other math function support in SPARQL

送分小仙女□ 提交于 2019-12-10 14:38:58
问题 I am trying to write a SPARQL query where I want to filter on the square of something, but I am simply unable to figure out how to square a number ( x 2 ) (except by multiplying it with itself, of course). I guessed a square root function called math:sqrt() which works, yet nothing like math:pow seems to exist. How do I get the square of something in SPARQL and, more importantly, where can I read about it and other math functions such as math:sqrt in SPARQL? Note: This is related to my

Calculating large powers of a double with modulus 1000,000,007

陌路散爱 提交于 2019-12-10 12:13:56
问题 I want to find large powers of (1+sqrt(3))^(n+1) where n varies from n=1 to n=1000,000,000. Can modular exponentiation by squaring work with doubles? How? I have searched a lot but no positive results yet.Any help will be highly appreciated? 回答1: The answer to the question “Can modular exponentiation by squaring work with doubles?” is no, given some assumptions I will explain. Operations such as taking a residue modulo 1,000,000,007 are typical of various number theory calculations and other

Fast Exponentiation for galois fields

房东的猫 提交于 2019-12-10 08:09:02
问题 I want to be able to compute g^x = g * g * g * ... * g (x times) where g is in a finite field GF(2^m). Here m is rather large, m = 256, 384, 512, etc. so lookup tables are not the solution. I know that there are really fast algorithms for a similar idea, modpow for Z/nZ (see page 619-620 of HAC). What is a fast, non-table based way to compute cycles (i.e. g^x)? This is definitely a wishful question but here it comes: Can the idea of montgomery multiplication/exponentiation be 'recycled' to

What is the Prolog operator ^?

我与影子孤独终老i 提交于 2019-12-10 03:47:00
问题 What is the Prolog operator ^ ? Looking at The Prolog Built-in Directive op gives a list of the built-in operators. I see ** is exponentiation /\ is or but what is ^ ? Each of the three current answers are of value and I learned something: Roy for the book false for the examples I accepted the answer by CapelliC because it made clear that ^/2 has multiple meanings depending on context which instantly cleared up my confusion. 回答1: In Prolog, most symbols can be used 'uninterpreted', at

Modular Exponentiation in Java

假如想象 提交于 2019-12-07 06:33:29
问题 I need a way to calculate: (g^u * y^v) mod p in Java. I've found this algorithm for calculating (g^u) mod p: int modulo(int a,int b,int c) { long x=1 long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return (int) x%c; } and it works great, but I can't seem to find a way to do this for (g^u * y^v) mod p as my math skills are lackluster. To put it in context, it's for a java implementation of a "reduced" DSA - the verifying part requires this to be

Fast Exponentiation for galois fields

假如想象 提交于 2019-12-05 18:17:03
I want to be able to compute g^x = g * g * g * ... * g (x times) where g is in a finite field GF(2^m). Here m is rather large, m = 256, 384, 512, etc. so lookup tables are not the solution. I know that there are really fast algorithms for a similar idea, modpow for Z/nZ (see page 619-620 of HAC ). What is a fast, non-table based way to compute cycles (i.e. g^x)? This is definitely a wishful question but here it comes: Can the idea of montgomery multiplication/exponentiation be 'recycled' to Galois fields? I would like to think so because of the isomorphic properties but I really don't know.

How to raise a number to a power?

天涯浪子 提交于 2019-12-05 13:18:54
问题 I was trying to raise an integer to a power using the caret operator ( ^ ), but I am getting surprising results, e.g.: assert_eq!(2^10, 8); Searches on DuckDuckGo and Google didn't reveal anything about it. How can I perform exponentiation in Rust? 回答1: The caret operator ^ is not used for exponentiation, it's the bitwise XOR operator. Rust provides exponentiation via methods pow and checked_pow which guards against overflows. Thus, to raise 2 to the power of 10, do: let base: i32 = 2; // an