-4
\$\begingroup\$

Declare a numeric variable that is not floating point and do an operation that changes the value with the largest amount of difference so the result of the variable is far from the original value.

Solutions will be scored based on the magnitude of the difference between initial value and the value after changed, and shortness of code.

Edit:
I realize that this was a bit vague - sorry about that, it's my first try on code golf...

I want to also emphasize that the result must not cause overflow. So once the variable is declared with a specific type (which must not be floating point), the result would have to be valid within that type.

\$\endgroup\$
  • \$\begingroup\$ Is the problem the following: Given an integer N, return an integer D such that max(|N-D|,|D-N|) is maximized? \$\endgroup\$ – Thomas Eding Oct 14 '11 at 1:00
  • 3
    \$\begingroup\$ You do realise that there are languages which allow arbitrarily large integers? \$\endgroup\$ – Peter Taylor Oct 14 '11 at 7:27
  • \$\begingroup\$ @trinithis: Not "return an integer". You must change the value of an integer variable that results in max diference. \$\endgroup\$ – awe Oct 14 '11 at 8:00
  • \$\begingroup\$ @PeterTaylor: No, I just realized it with the Phyton answer by JiminP. I have not very broad experience with such languages, and in my narrow mind, a numeric type has bounds, so I'm feeling his answer is cheating, but... \$\endgroup\$ – awe Oct 14 '11 at 11:38
  • 3
    \$\begingroup\$ It is not an objective winning criterion, if you have 2 goals to reach. Person A might have the shortest code, Person B the greatest difference, Person C might have something in between for both aspects. You could solve the problem by a combined measure: Maximise (100-code-size)*difference, or by ordering the winning criteria: Shortest code wins. For two equal shortest codes, the bigger difference wins. Prepare your questions on meta or in chat next time, to solve such problems and ambiguities before starting the quiz. \$\endgroup\$ – user unknown Oct 15 '11 at 16:37
3
\$\begingroup\$

Java (55 26 19 chars, Diff: 2^64-1 = 18446744073709551615)

Given a very unclear spec on what we're supposed to be doing, here's a best try :-) Obviously I will easily be beat out by languages that have arbitrary precision integers like Python or APL/J.

long o=Long.MIN_VALUE;o+=Long.MAX_VALUE+Long.MAX_VALUE;

Demo: http://www.ideone.com/7HyCW

EDIT: Shorter version, making use of Long underflow to wrap around from MIN_VALUE to MAX_VALUE (26 chars):

long o=Long.MIN_VALUE;o--;

Demo: http://www.ideone.com/Qfao7

EDIT 2: Shortest yet taking into account @ratchet freak's suggestions:

long o=1L<<63;o=~o;

Demo: http://www.ideone.com/2M9oa

|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ wouldn't o=~o; also work without overflow (and isn't 1<<63 or -1>>>1 a shorter initialization) \$\endgroup\$ – ratchet freak Oct 13 '11 at 18:23
  • 1
    \$\begingroup\$ @ratchetfreak: Updated with your suggestion. Note have to use 1L<<63 otherwise it overflows at 2^32 because 1 is integer by default. \$\endgroup\$ – mellamokb Oct 14 '11 at 5:28
2
\$\begingroup\$

D (15 chars 2^64-1 difference)

ulong o=0;o=~o;

uses unsigned long and logical NOT to get max value

note that using ucent would get me to 2^128-1 difference (if it were to be implemented at least)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Just curious - what is ucent? \$\endgroup\$ – awe Oct 14 '11 at 8:11
  • \$\begingroup\$ @awe a 128 bit unsigned integer but it's not yet part of the language (it's planned for when processors go 128 bit) \$\endgroup\$ – ratchet freak Oct 14 '11 at 8:39
  • \$\begingroup\$ You will have one less char if you do o-- instead of o=~o. It will both result in the inverted value, because o-- will "wrap around" and use the max value of ulong (at least in C# where I tested it). \$\endgroup\$ – awe Oct 14 '11 at 12:05
  • \$\begingroup\$ I haven't found the wrapping rules of unsigned types in D (which I think would undefined like in C) \$\endgroup\$ – ratchet freak Oct 14 '11 at 14:58
1
\$\begingroup\$

Haskell - 8 characters (Infinite difference)

f=f.(+1)

Use f on Integer.

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ Strictly speaking, you defined a function; you didn't "do an operation." \$\endgroup\$ – J B Oct 14 '11 at 6:21
  • \$\begingroup\$ Heh, well this problem cannot be done in Haskell otherwise. Yes, there are MVars and such, but that doesn't exactly cut it either. \$\endgroup\$ – Thomas Eding Oct 14 '11 at 19:56
  • \$\begingroup\$ The closest I can think of would be an action, but my main point is: from within Haskell, it's not possible to even verify that yours yields a number greater than the one it was given. \$\endgroup\$ – J B Oct 14 '11 at 20:16
1
\$\begingroup\$

Golfscript

2{.?}9*

7 chars, unimaginably large difference. (The loop executes 9 times. The first time maps 2 -> 2^2 = 4. The second time maps 4 -> 4^4 = 256. The third time maps 256 -> 256^256 ~= 3.23 * 10^616. The fourth time overflows memory, but not the datatype...)

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ The requrements was to first declare a numeric variable (not float), then do the operation(s) on that variable. \$\endgroup\$ – awe Oct 14 '11 at 8:55
  • \$\begingroup\$ @awe, in GolfScript a variable is just a value on the stack. If you want to write something which isn't GolfScript in GolfScript I suppose you could do 2:a;{a.?:a;}9* \$\endgroup\$ – Peter Taylor Oct 14 '11 at 9:08
  • \$\begingroup\$ ... which would be 14 chars? \$\endgroup\$ – user unknown Oct 15 '11 at 16:24
  • \$\begingroup\$ @userunknown, yes, like that. The problem is that the challenge isn't well enough specified for me to know how many of those characters are necessary; this was more a protest answer than anything. \$\endgroup\$ – Peter Taylor Oct 15 '11 at 21:21
1
\$\begingroup\$

J - 16 characters

a=.<.<:(a=.2)^31

The value of a changes from 2 to 2147483647. The calculation makes the value become float, but the final result is integer.

There are also extended integers, but I don't know if they would be acceptable.

a=.(a=.0)+!100000x

Yes, that really spells out mathematically as !100000. J can crunch it (in a few minutes), brute force.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

C with SSE2 (79 chars, Difference: 2^128-1 = 3.4e38)

#include<emmintrin.h>
main(){__m128i v=_mm_set1_epi8(-1);v=_mm_xor_si128(v,v);}

This initially sets the 128 bit unsigned int v to its maximum value, and then changes this value to 0 by XORing v with itself.

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Python (13 chars, Difference: 9^(9^9) (369,693,100 digits!))

a=0;a=9**9**9

I believe that the result of this operation is not a float, isn't it?

If that's problem because it doesn't yield the result...

Mathematica (11 chars, Difference: only 15,151,335 digits)

8^8^8

If it's still a problem because this value is not the maximum possible, then, a=2^1073741696;a*=-1 (20 chars) will do the work.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ What type is a declared as? (implicit declaration?) Is this not causing overflow on the type of a? \$\endgroup\$ – awe Oct 14 '11 at 8:09
  • \$\begingroup\$ @awe Python has arbitrary-precision integer. For example, 4**4**4 returns 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096L. (PS: It is 4**(4**4); (4**4)**4 returns only 4294967296L.) \$\endgroup\$ – JiminP Oct 14 '11 at 11:09
  • \$\begingroup\$ I added the calculation of Mathematica. If this is a problem because the difference isn't the maximum possible, then I'll go with a=2^1073741696;a*=-1 \$\endgroup\$ – JiminP Oct 14 '11 at 11:24
  • \$\begingroup\$ arbitrary-precision integer: I feel cheated... \$\endgroup\$ – awe Oct 14 '11 at 11:43
0
\$\begingroup\$

bc 15chars:

On my system I can go until 8^8^8 without error, but 6^6^6 would already be too long to report it here, so take 5^5^5 as an example; 7^7^7 is in the range of one minute on a single core 2Ghz machine. With 8 you should have some time left.

echo "k=8^8^8;k*=-1;k" | bc 

printable result for one page:

echo "k=5^5^5;k*=-1;k" | bc 

-1911012597945477520356404559703964599198081048990094337139512789246\ 52053024261580301205938651973985026558644015579446223535921278867380\ 69722884101469159866020879618967571957018392816603380476112259755336\ 26101001482651123413147768252411493094447176965282756285196737514395\ 35754247909321920664188301178716912255242107005070906467438287085144\ 99502565861944615431835113798491336917799281274338404315492368555267\ 83596374102105331546031353725325748636909159778690328266459182983815\ 23028693657287369142264813129174376213632573032164528297948686257624\ 53622180176732249405676428193600787207138370723553054463561539464011\ 85348493792719514594505508232749221605848912910945189959948686199543\ 14766693801303717616359259447974616422005088507946980448713320513316\ 07391342305401988725700383298012460501970134673971759090273894939238\ 17315786996845899794781068042822436093783946335265422815704302832442\ 38551508231649096728571217170812323279048181726832751011274678231741\ 09858886837085220007117334922539133223007561471804290075276777933523\ 06200618286012455254243061006894805446584704820650982664319360960388\ 73625851074707434063628697657670269925864995355797631817390255089133\ 12232947439303439561613283340728316634982581452268620043077990846881\ 03804187368324800903873596212919633602583120781673673742533322879296\ 90720549059562140688882599124458184237959786347648431567376092362509\ 03715117989414242622702200662864868678687101829808728025606931019492\ 80830825044198424796792058908817112327192301455582916746795197430548\ 02640464685400273399386079859446596150175258696581144756851004156868\ 77309037124825353438392853975987494584970500382250124892840018265900\ 56251286187629938044407340142347062055785305325034918189589707199305\ 66218851296318750174353596028220103821161604854512103931331225633226\ 07664362366882968502088394961428304847391139916696226499485636852347\ 12873294796680884509405893951104650944137909502276545653133018670633\ 52132302846051943438139981056140065259530073179077271106578349417464\ 26847209561346473277485842382748996687550525043942182321913572230540\ 66715373374248543645663782045701654593218154053548393614250664498585\ 40330746646854189014813434771465031503795417577862281177658587694168\ 0908203125

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

BrainF***, 3 characters, theoretically unlimited difference

[+]

Some implementations of BF wrap. "True" BF allows for unlimited integers. This has a difference of whatever you want it to be, plus it's only 3 characters long :)

|improve this answer|||||
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.