Jump to content

So I came across this problem on a computer on which some grade 5 kids had had programming the hour before I came into the class.

 

Determine all the numbers which have this property :

The square of the sum of its digits is equal to the number itself, which must have at least 2 digits and is natural.

 

A few minutes later, I realized what a dumb programming problem it was.

Seriously...the only number which fulfills that property is 81.

Yup.

Only 1 number.

My amazing solution in O(1) :

cout << 81;

I know that programming problems which can be solved with just 1 formula are kinda bad, but this one...lol.

 

 

Well, at least I discovered an interesting property of the number 81.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/
Share on other sites

Link to post
Share on other sites

Ummm, no? 

 

Let's assume it's 81. 

 

Then the square of the sum of it's digits (9) would be 3 =/= 81. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444009
Share on other sites

Link to post
Share on other sites

Ummm, no? 

 

Let's assume it's 81. 

 

Then the square of the sum of it's digits (9) would be 3 =/= 81. 

square  not square root. 

CPU: Intel 3570 GPUs: Nvidia GTX 660Ti Case: Fractal design Define R4  Storage: 1TB WD Caviar Black & 240GB Hyper X 3k SSD Sound: Custom One Pros Keyboard: Ducky Shine 4 Mouse: Logitech G500

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444017
Share on other sites

Link to post
Share on other sites

Ummm, no? 

 

Let's assume it's 81. 

 

Then the square of the sum of it's digits (9) would be 3 =/= 81.

It's squared, not sqrt.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444020
Share on other sites

Link to post
Share on other sites

well cout<<81 is not actually a formula...thats just directly outputting the answer

 

you would need to actually write code to go through and check every number and then output the answer once it finds it

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444037
Share on other sites

Link to post
Share on other sites

well cout<<81 is not actually a formula...thats just directly outputting the answer

 

you would need to actually write code to go through and check every number and then output the answer once it finds it

Fine then.

int answer = 81;

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444048
Share on other sites

Link to post
Share on other sites

 

Fine then.

int answer = 81;

that's still not a formula...

it doesnt find anything

youre just giving it an answer

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444055
Share on other sites

Link to post
Share on other sites

0 and 1

 

google it. 

Numbers with at least 2 digits, forgot to mention.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444058
Share on other sites

Link to post
Share on other sites

 

Fine then.

int answer = 81;

 

Less efficent

 

that's still not a formula...

it doesnt find anything

youre just giving it an answer

A good program is a lazy program. (least effort)

"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard Fynman

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444059
Share on other sites

Link to post
Share on other sites

that's still not a formula...

it doesnt find anything

youre just giving it an answer

But there is , mathematically , no other number with that property. What's the purpose of searching for others?

The formula is literally

f(x) = 81

That's the most efficient way to do it , of course.

You can still go ahead and check the proprieties for all numbers up to a certain limit, but that would be pointless.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444062
Share on other sites

Link to post
Share on other sites

But there is , mathematically , no other number with that property. What's the purpose of searching for others?

The formula is literally

f(x) = 81

That's the most efficient way to do it , of course.

You can still go ahead and check the proprieties for all numbers up to a certain limit, but that would be pointless.

but its not the program solving that problem, its you

the only thing the program does is print it to the screen

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444116
Share on other sites

Link to post
Share on other sites

But there is , mathematically , no other number with that property. What's the purpose of searching for others?

The formula is literally

f(x) = 81

That's the most efficient way to do it , of course.

You can still go ahead and check the proprieties for all numbers up to a certain limit, but that would be pointless.

Programming competitions (yes they exist, NZ programming competition for example) are judged on how many problems you can solve (with varying points). The check to see if the program works simply throws a ton of expected and boundary inputs at it and sees if the out put is correct. The original program would do that, plus efficiency score for a tie breaker. 

"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard Fynman

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444141
Share on other sites

Link to post
Share on other sites

but its not the program solving that problem, its you

the only thing the program does is print it to the screen

So if I were given this problem :

Find all prime numbers which are also even.

I should go ahead and check through all numbers and see if it's prime and even at the same time  ? Of course not, the only solution is 2.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444155
Share on other sites

Link to post
Share on other sites

but its not the program solving that problem, its you

the only thing the program does is print it to the screen

It,s just a bad problem.

 

Would make for a decent math one where you have to prove that 81 is the only answer but not a programming problem.

"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard Fynman

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444157
Share on other sites

Link to post
Share on other sites

Programming competitions (yes they exist, NZ programming competition for example) are judged on how many problems you can solve (with varying points). The check to see if the program works simply throws a ton of expected and boundary inputs at it and sees if the out put is correct. The original program would do that, plus efficiency score for a tie breaker. 

Exactly.

Here we focus on programming competitions and heavily on efficiency of programs.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444165
Share on other sites

Link to post
Share on other sites

So if I were given this problem :

Find all prime numbers which are also even.

I should go ahead and check through all numbers and see if it's prime and even at the same time  ? Of course not, the only solution is 2.

That wouldn't teach the same lesson (which was presumably bit shifting -- or being lazy and just separating numbers). (albeit there are definitely better ways to teach the same lesson...but whatever)

int main(int argc, char ** argv){    int x = 0, y=0;    for(int i=10 ; i<100; i++){ //yes, I could do something a bit more complex that will                                 //calculate the length of the number, but I'm lazy           x = i/10;           y = (i % x*10)/10;           if((x+y)*(x+y) == i)                 printf("ANSWER: %d\n", i);    }    return 0; }

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444237
Share on other sites

Link to post
Share on other sites

So if I were given this problem :

Find all prime numbers which are also even.

I should go ahead and check through all numbers and see if it's prime and even at the same time  ? Of course not, the only solution is 2.

yes you should

because a computer doesn't know that 2 is the only prime number

only you do

 

having a computer solve a problem by you putting the answer into it is not making the computer solve a problem

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444342
Share on other sites

Link to post
Share on other sites

If a competition wants to ensure that the competitors actually used the concept, then one possibility would be to add the requirement of the code working for any non-decimal numeral system.

which would make a quite good problem.

Input: base then number of digits

Output: possible numbers where their root=sum of digits.

Example:

10

2

81

"Physics is like sex: sure, it may give some practical results, but that's not why we do it." Richard Fynman

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444371
Share on other sites

Link to post
Share on other sites

yes you should

because a computer doesn't know that 2 is the only prime number

only you do

 

having a computer solve a problem by you putting the answer into it is not making the computer solve a problem

But Maths is still Maths, doesn't matter if it's a computer doing it or a human.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444476
Share on other sites

Link to post
Share on other sites

But Maths is still Maths, doesn't matter if it's a computer doing it or a human.

no...

if the point of the problem is to solve something using code, solving it yourself and inputting the answer so that the code prints it is not making the code solve the problem

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/480362-this-programming-problem/#findComment-6444495
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×