Page 1 of 1

Qbasic problem! Please help!

Posted: Sun May 26, 2013 3:55 am
by James
OK, I think QBasic, is an interesting programming language... I am only just a starter though, so I have some difficulties. Yesterday I saw a really interesting problem: "Number 1634 is really interesting, because it can be expressed as 1634 = 1^4 + 6^4 + 3^4 + 4^4. Write the program that can find other numbers like 1634"

Posted: Sun May 26, 2013 2:17 pm
by burger2227
Sounds like homework to me. I'll let it slide though if you post some attempts at a solution.

You will need to scan the digits as text and convert them to numerical values to work with the exponent values then convert that value back to see if they match.

Posted: Mon May 27, 2013 1:41 pm
by James
burger2227 wrote:Sounds like homework to me. I'll let it slide though if you post some attempts at a solution.

You will need to scan the digits as text and convert them to numerical values to work with the exponent values then convert that value back to see if they match.
well I tried this...
IF a*1000 + b*100 + c*10 + d = a*a*a*a + b*b*b*b + c*c*c*c + d*d*d*d THEN
PRINT a*1000+b*100+c+10+d
END IF
But, I just get 0 as an answer!

Posted: Mon May 27, 2013 2:06 pm
by burger2227
I'd assume that the exponent is based on the number of digits in the number. Four digit numbers would be an exponent of 4.

You can use ^ to create an actual exponential value such as a = 4 ^ 4

First you need to convert the test number to a string to find each digit.

testnumber$ = LTRIM$(STR$(number)) 'removes leading space

exponent = LEN(testnumber$)

FOR digit = 1 TO exponent
'use MID$ to find each digit of the test number, VAL it and make the exponent value and add it to a total.
NEXT

Then compare the total to the original value. You would need to test a range of numbers like from 1000 to 9999.

Re: Qbasic problem! Please help!

Posted: Mon Jul 22, 2013 5:33 pm
by Jack002
Hi, I'm Jack T. New here, not to QuickBasic.

I see this post was from last MAY, so warning, its a reply to an old post.

I see this might be from a homework assignment, given the age of the question, I think we're ok now. Its safe.

I wanted to try out this puzzle, how many cases are true from 1000 to 9999 where the first digit^4 + second^4 + third^4 + fourth^4 = itself? (Ignoring the cases of numbers not of 4 dec places) This is what I came up with

Code: Select all

'test to see if a number's digits in the form
' a^4 + b^4 + c^4 + d^4 = itself.
'in the range from 1000-9999

FOR a = 1000 TO 9999
    t1 = VAL(MID$(STR$(a), 2, 1))
    t2 = VAL(MID$(STR$(a), 3, 1))
    t3 = VAL(MID$(STR$(a), 4, 1))
    t4 = VAL(MID$(STR$(a), 5, 1))
    IF t1 ^ 4 + t2 ^ 4 + t3 ^ 4 + t4 ^ 4 = a THEN PRINT a
NEXT a
Only three numbers qualify.

Jack T.

PS: If you ever saw "Numberphile" in YouTube, this is right up their alley. Check it out.