Jump to content


Photo

Math/Logic Help?


  • Please log in to reply
13 replies to this topic

#1 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 04:27 PM

**SOLVED**

Sorry if this isn't appropriate for here, but I can't figure out how to do this.
I have two variables, a&b. I want to establish a percentage between 15-35 based on a&b. So if:

$a = 0;
$b = 10;
$percent = 15%


$a = 5;
$b = 10;
$percent = 25%

$a = 10;
$b = 10;
$percent = 35%

To summarize: the closer A gets to B, the higher the percentage is.

So stuck on how to do this? Any help would be GREATLY appreciated, thank you SO much!
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 04 April 2006 - 05:48 PM

Hi
i'd hazard a guess that this is purely a math question rather than php at all, but i can't see any way that $a=1 and $b=10 could ever create 15%, even by using 35 instead of 100 to work out the percentage?...

maybe you could elaborate a bit more?
Cheers
Mark

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 05:57 PM

[!--quoteo(post=361626:date=Apr 4 2006, 12:48 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 12:48 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hi
i'd hazard a guess that this is purely a math question rather than php at all, but i can't see any way that $a=1 and $b=10 could ever create 15%, even by using 35 instead of 100 to work out the percentage?...

maybe you could elaborate a bit more?
Cheers
Mark
[/quote]

I know, but I couldn't find anywhere else to ask, and I am trying to do it in PHP ;)

Let me try to explain better. Depending on how close A is to B, I want to pick a number between 15-35. We could make it just 0-20 and add on 15 later.

So if B is 20, it's easy:
A=1, number = 0, so +15 = 15%
when A = 10, number = 10, 25%.
When A is 20, number=20 +15 = 35%

Make sense?

It's easy when B is 20, but if B is 7 and A is 4...
How do I find the number on the scale?


A..............B
0..............20
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 04 April 2006 - 05:57 PM

assuming that your examples are not shown with the results you expect, then:

$percent = ($a/$b)*20;
would give you a "percentage" with 20 as the top value, as the normal way to work out percent is:

$percent = ($pizza_slice_size/$pizza_full_size)*100;

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#5 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 05:59 PM

[!--quoteo(post=361630:date=Apr 4 2006, 12:57 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 12:57 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
assuming that your examples are not shown with the results you expect, then:

$percent = ($a/$b)*35;
would give you a "percentage" with 35 as the top value, as the normal way to work out percent is:

$percent = ($pizza_slice_size/$pizza_full_size)*100;
[/quote]

So if I want only between 15-35, I could do:
$percent = ($a/$b)*20; and add 15% right?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#6 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 04 April 2006 - 06:01 PM

[!--quoteo(post=361632:date=Apr 4 2006, 06:59 PM:name=jesirose)--][div class=\'quotetop\']QUOTE(jesirose @ Apr 4 2006, 06:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
So if I want only between 15-35, I could do:
$percent = ($a/$b)*20; and add 15% right?
[/quote]

yes. just watch the order things are calculated in though:

$percent = (( $a / $b ) * 20) + 15;

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#7 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 06:03 PM

[!--quoteo(post=361633:date=Apr 4 2006, 01:01 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 01:01 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
yes.
[/quote]

Thaaank you :)
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#8 djnrempel

djnrempel
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 April 2006 - 06:22 PM

I think you have some unstated constants in your problem, namely a $floor of 1 and a $ceiling of 10 (although $b seems to contain that constant)

Your problem is to convert a value between $floor and $ceiling to a value between 15 and 35. Basically, this is the same as converting celsius to fahrenheit - the units are of different size, and the scale starts at different points.

// First, find the scales, and the ratio between them:

$scale1= $ceiling-$floor; // will evaluate to 9 if $floor is 1, 10 if $floor is 0
$scale2= 35-15; // equals 20

$factor= $scale2 / $scale1; // if $scale is 9 this will be some ugly decimal, if it is 10 it will be a nice tidy 2

// now convert $a to value on $scale2:

$percentage = ($a - $floor) * $factor; // if $floor is 1 then if $a==1 $percentage will be 0
$percentage  += 15; // add the base of 15 to the percentage value

if you really have to have a floor of 1, I suggest using round() on your result.

#9 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 06:25 PM

Actually the way redbullmarky posted worked perfectly.

Here's what I get now when B = 7

0/7: 15
1/7: 17
2/7: 20
3/7: 23
4/7: 26
5/7: 29
6/7: 32
7/7: 35
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#10 jvrothjr

jvrothjr
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 04 April 2006 - 06:49 PM

SELECTED NUMBER A 5.0000
STARTING NUMBER B 1.0000
ENDING NUMBER C 10.0000
STARTING PERCENT D 15.0000
ENDING PERCENT E 35.0000
PERCENT DIFFERENCE F 20.0000 E - D = F
PERCENT PER STEP G 2.2222 F / (C -B) = G
PERCENT AT NUMBER H 23.8889 ( G * ( A - B ) ) = H


This was a little odd had to think on this one lol....... bad I know

but I think we got it

1) You need to define a min / max number [C - B = steps in range]
2) You need to define a min / max percent [E - D = F {Percent per range}]
3) [F / (C -B) = G {percent per step}]
4) [( G * ( A - B ) ) = H {to get you percent per given number}]

if 1 was 15 and 10 was 35 then 5 would have been 23.8889
so if you define start and finish points then you could get the percent with in that range and the formula will get you your percentage via number given


If you understand everything you know nothing!

http://rcchjr.awardspace.com/

#11 djnrempel

djnrempel
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 04 April 2006 - 08:17 PM

[!--quoteo(post=361645:date=Apr 4 2006, 01:25 PM:name=jesirose)--][div class=\'quotetop\']QUOTE(jesirose @ Apr 4 2006, 01:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Actually the way redbullmarky posted worked perfectly.

Here's what I get now when B = 7

0/7: 15
1/7: 17
2/7: 20
3/7: 23
4/7: 26
5/7: 29
6/7: 32
7/7: 35
[/quote]

But it would not work if you wanted 1/10 to be 15%, it would have made 1/10 equal to 17% (15 + (1/10 *20)) But I see now that you wanted 0 to be 15% - did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)

#12 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 08:20 PM

[!--quoteo(post=361708:date=Apr 4 2006, 03:17 PM:name=Dan R.)--][div class=\'quotetop\']QUOTE(Dan R. @ Apr 4 2006, 03:17 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
But it would not work if you wanted 1/10 to be 15%, it would have made 1/10 equal to 17% (15 + (1/10 *20)) But I see now that you wanted 0 to be 15% - did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)
[/quote]

That was my fault, I fixed it to reflect it. I've got it working perfectly now. I meant when A=0, not a=1 :) Sorry!
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#13 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 04 April 2006 - 09:03 PM

[!--quoteo(post=361708:date=Apr 4 2006, 09:17 PM:name=Dan R.)--][div class=\'quotetop\']QUOTE(Dan R. @ Apr 4 2006, 09:17 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
...did you edit that or was it just Mark who started all this 1/10 = 15% nonsense ;)
[/quote]

lol well i would have loved to have used pythagoras theorem or some celsius/farenheight mathematical black hole to solve the problem, but i thought i'd just stick to the question that was asked in the first place ;) hey, if someone wants a percentage to start at 15 and end at 35, who am i to argue?
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#14 Jessica

Jessica
  • Staff Alumni
  • This is not my name.
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 04 April 2006 - 09:29 PM

[!--quoteo(post=361729:date=Apr 4 2006, 04:03 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Apr 4 2006, 04:03 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
hey, if someone wants a percentage to start at 15 and end at 35, who am i to argue?
[/quote]

I do have a reason ;) I promise, I'm not totally insane.
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users