The result of a numeric operation was too large.

It’s probably a long shot, but is there a way around this error? It’s highly annoying. I’ve ran into it twixe so far. Once when trying to do RSA encryption, and now again while testing Carmichael numbers. I don’t want to break my simple equations down into smaller ones…

Aren’t Carmichael Numbers the pseudo primes? I vaguely recall that they get large pretty quickly, so you’d need multiple precision arithmetic to calculate them. AppleScript doesn’t support more than single precision (unless someone has written an OSAX for higher precision).

Yes, they’re numbers that pass the Fermat test, but are not prime.

They start at 561.

My script is theoretically sound. It works in terms of testing Prime, and testing Carmichael, yet I can’t see if Carmichael works properly because it get this error. I suppose i could just go on not knowing, and I’m pretty sure it works, I’m just one of those people who “have to know”.

I am a bit out of my league here, but could you use the shell and pipe your equations to ‘bc’ ?

I believe that bc can handle quite large integers without modification.

Andy

I <3 you. I mean that in the most non-weird way. :stuck_out_tongue:

code!

global buttonPressed

property carmichaelness : false
property primeness : false
property isPrime : false

main()

on main()
	set buttonPressed to "x"
	display dialog "Enter a number:" default answer "" buttons {"Cancel", "OK"} default button "OK"
	try
		set theNumber to text returned of result as number
		
	on error
		display dialog "I said enter a number." buttons {"OK"} default button "OK" with icon stop
		restartScript()
	end try
	
	my testCarmichael(theNumber)
	my testPrimeness(theNumber)
	
	if carmichaelness and not primeness then
		display dialog theNumber & " is a Carmichael number." as string buttons {"OK"} default button "OK" with icon 1
		restartScript()
	else
		display dialog theNumber & " is normal." as string buttons {"OK"} default button "OK" with icon 1
		restartScript()
	end if
end main

on testCarmichael(testCar)
	if testCar ≠ 1 then
		set testCar2 to testCar - 1
		set randomNumber to random number from 2 to testCar2
		set theResult to "echo '" & randomNumber & " ^ " & testCar & " % " & testCar & "' | bc" as string
		set theResult to do shell script theResult
		set theResult to theResult as number
		if theResult is equal to randomNumber then
			set carmichaelness to true
		else
			set carmichaelness to false
		end if
	else
		set carmichaelness to false
	end if
end testCarmichael

on testPrimeness(testPrime)
	if class of testPrime is real then
		display dialog "Real numbers cannot be prime." buttons {"OK"} default button "OK" with icon stop
		copy the result as list to {buttonPressed}
	end if
	if testPrime < 0 then
		set theFlag to -1
	else
		set theFlag to 1
	end if
	
	if testPrime is in {2, -2} then
		set testPrime to testPrime as string
		display dialog testPrime & " is a prime number, but not a Carmichael number." buttons {"OK"} default button "OK"
		restartScript()
	else if testPrime is in {1, 0, -1} then
		set testPrime to testPrime as string
		display dialog testPrime & " is not a prime, or Carmichael number." buttons {"OK"} default button "OK"
		restartScript()
	end if
	
	if (testPrime mod 2) = 0 then
		if testPrime ≠ "2" then
			display dialog "Even numbers whos magnitude are greater than 2 are never prime." buttons {"OK"} default button "OK" with icon stop
			copy the result as list to {buttonPressed}
		end if
	end if
	repeat with inc from 3 to (2 * (((testPrime * theFlag) ^ (1 / 2)) div 2) + 3) by 2
		if (testPrime mod inc) = 0 then
			set testPrime to testPrime as string
			set primeness to false
			display dialog testPrime & " is not a prime number as it has a divisor of " & inc buttons {"OK"} default button "OK"
			copy the result as list to {buttonPressed}
			
		end if
	end repeat
	if buttonPressed = "OK" then
		set isPrime to false
	else
		set isPrime to true
	end if
	
	if isPrime = true then
		set testPrime to testPrime as string
		set primeness to true
		display dialog testPrime & " is a prime number." buttons {"OK"} default button "OK"
	end if
end testPrimeness

on restartScript()
	display dialog "Do you want to go again?"
	main()
end restartScript

This is pretty cool. On my dual 2.7 GHz G5 it took about 4 minutes to do 825265. I didn’t try any higher. How high are you trying to go?

I did not try:

999987515379253441

:wink:

Andy

Browser: Safari 412
Operating System: Mac OS X (10.4)

Slick, but requires patience. I’ve had 1234567 running for a long time now and it’s eating cpu cycles in bc without a result so I can’t tell if it’s stuck, or just munching away. There is another calculator in Darwin calle dc; “a reverse-polish desk calculator which supports unlimited precision arithmetic”. Maybe it’s faster (haven’t used it).

Honestly I was just doing the first few numbers as part of a challenge. As for dc, it’s stack based. So it looks like “3 4 *” to get 3 * 4. It’s just too weird for me.